现版本 SDK 8.4 Xcode

运行Xcode 选择 Create a new Xcode project ->Single View Application 命名 NavigationControllerDemo

一、创建 Empty Application

把工程目录下的Main.storyboard和LaunchScreen.xib删除,扔进废纸篓。(ViewController.h ViewController.m也可删除)

打开Info.plist,把Launch screen interface file base name,以及Main storyboard file base name两项,(点击旁边的减号即可)删除。

打开工程项目属性文件,点击Target下面的第一项,再选择General选项卡,向下找到Use Asset Catalog按钮。点击它。

弹出对话框,点击Migrate即可。这样,应用尺寸就能根据屏幕大小进行调整了。

在AppDelegate的第一个方法里面即

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

,“return”语句之前,添加必要代码。

//创建window

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//设置window背景

self.window.backgroundColor = [UIColor whiteColor];

//使window可见

[self.window makeKeyAndVisible];

编译运行通过,出现空白页面。

二、创建一个View Controller,作为Root View Controller:依次选择File——New——New File,在弹出的窗口,左边选择iOS下的Source,右边选择Cocoa Touch Class

单击Next,在新窗口输入名称为RootViewController,sub of选择UItableViewController完成创建

三、AppDelegate添加如下代码

打开AppDelegate.h

添加  @property (strong, nonatomic) UINavigationController *navController;

打开AppDelegate.m 红色为添加的代码

#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

@synthesize navController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

//创建window

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

RootViewController *root = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];

self.navController = [[UINavigationController alloc] initWithRootViewController:root];

[self.window addSubview:navController.view];

//设置window背景

self.window.backgroundColor = [UIColor whiteColor];

//使window可见

[self.window makeKeyAndVisible];

return YES;

}

@end

四、Root View Controller中是一个表格,它的每一行对应一个Sub View Controller

打开RootViewController.h,添加属性:

@property (strong, nonatomic) NSArray *controllerList;

打开RootViewController.m,在@implementation之后添加代码:@synthesize controllerList;

在viewDidLoad中[super viewDidLoad];之后添加代码:

self.title = @"分类";

NSMutableArray *array = [[NSMutableArray alloc] init];

self.controllerList = array;

找到numberOfSectionsInTableView方法,修改其返回值为。

找到numberOfRowsInSection:方法,修改代码为:return [controllerList count];

找到cellForRowAtIndexPath方法,修改其中代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *RootTableViewCell = @"RootTableViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: RootTableViewCell];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: RootTableViewCell];

}

NSUInteger row = [indexPath row];

UITableViewController *controller = [controllerList objectAtIndex:row];

//这里设置每一行显示的文本为所对应的View Controller的标题

cell.textLabel.text = controller.title;

//accessoryType就表示每行右边的图标

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

}

找到didSelectRowAtIndexPath:方法,修改其中代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSUInteger row = [indexPath row];

UITableViewController *nextController = [self.controllerList objectAtIndex:row];

[self.navigationController pushViewController:nextController animated:YES];

}

五、controllerList这个数组现在是空的,目前为止运行之后还是看不到任何效果

接下来创建一个Table View Controller,用于显示电影列表。建立的方法同建立RootViewController一样,名称为MovieViewController。

之后再创建一个View Controller,名称为MovieDetailViewController,用于显示电影的详细信息Subof选项为UIViewController:

最后再创建一个Storyboard 命名为Main(创建空工程时若保留Storyboard,此时可不再重复创建)

六、布局MovieDetailViewController

选中Main.storyboard  选择: Tools -> Library ,从Library显示菜单中拖拉一个View Controller 到 故事版(storyboard)

选中 View Controller  Identity->Custom Class->Class->MovieDetailViewController(将view controller 页面 和MovieDetailViewController文件 关联)

命名Storyboard ID为 MovieDetail 勾选Use Storyboard ID (动态Scene绑定时有用)

选择: Tools -> Library ; 从Library显示菜单中拖拉一个 Label 到 Movie Detail View Controller

将其映射到MovieDetailViewController.h中,名称为detailLabel

然后在MovieDetailViewController.h中添加属性:

@property (copy, nonatomic) NSString *message;

打开MovieDetailViewController.m,在@implementation之后添加代码:@synthesize message;

在viewDidLoad方法后面添加一个方法:

- (void)viewWillAppear:(BOOL)animated {

detailLabel.text = message;

[super viewWillAppear:animated];

}

viewWillAppear这个方法每次视图加载都会执行,而viewDidLoad方法只有在第一次加载时才会执行。

七、打开MovieViewController.h,向其中添加属性:

@property (strong, nonatomic) NSArray *movieList;

打开MovieViewController.m,在@implementation之前添加代码:

#import "MovieDetailViewController.h"

#import "AppDelegate.h"

@interface MovieViewController ()

@property (strong, nonatomic) MovieDetailViewController *childController;

@end

在@implementation之后添加代码:

@synthesize movieList;

@synthesize childController;

在viewDidLoad方法中添加代码:

NSArray *array = [[NSArray alloc] initWithObjects:@"肖申克的救赎", @"教父", @"教父:II",

@"低俗小说", @"黄金三镖客", @"十二怒汉", @"辛德勒名单",

@"蝙蝠侠前传2:黑暗骑士", @"指环王:王者归来", @"飞越疯人院",

@"星球大战Ⅴ:帝国反击战", @"搏击俱乐部", @"盗梦空间", @"七武士",

@"指环王:护戒使者", @"好家伙", @"星球大战IV:新希望", @"上帝之城",

@"卡萨布兰卡", @"黑客帝国", @"西部往事", @"后窗", @"夺宝奇兵",

@"沉默的羔羊", @"非常嫌疑犯", @"七宗罪", @"指环王:双塔奇兵", @"阿甘正传",

@"惊魂记", @"美好人生", nil];

self.movieList = array;

找到numberOfSectionsInTableView方法,修改其返回值为。

找到numberOfRowsInSection:方法,修改代码为:return [movieList count];

找到cellForRowAtIndexPath方法,修改其中代码如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MovieTableViewCell = @"MovieTableViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MovieTableViewCell];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: MovieTableViewCell];

}

NSUInteger row = [indexPath row];

NSString *movieTitle = [movieList objectAtIndex:row];

//这里设置每一行显示的文本为所对应的View Controller的标题

cell.textLabel.text = movieTitle;

//accessoryType就表示每行右边的图标

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

return cell;

}

修改didSelectRowAtIndexPath方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

在@end之前添加方法

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

if (childController == nil) {

//使用故事版 动态绑定scene

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

childController = [mainStoryboard instantiateViewControllerWithIdentifier:@"MovieDetail"];

}

NSUInteger row = [indexPath row];

NSString *selectedMovie = [movieList objectAtIndex:row];

NSString *detailMessage = [[NSString alloc]initWithFormat:@"你选择了电影:%@.", selectedMovie];

childController.message = detailMessage;

childController.title = selectedMovie;

[self.navigationController pushViewController:childController animated:YES];

}

八、打开RootViewController.m

在@implementation之前添加代码:#import "MovieViewController.h"

在viewDidLoad方法中self.controllerList = array;之前添加代码:

MovieViewController *movieViewController = [[MovieViewController alloc] initWithStyle:UITableViewStylePlain];

movieViewController.title = @"电影";

[array addObject:movieViewController];

九、选择: File -> Save

最后在 xCode 选择 Build and then Running

模拟器效果图

      

本文源于网上博客,经过本人修改和测试。原blog地址 http://justcoding.iteye.com/blog/1476196

代码下载:https://github.com/beike/NavigationControllerDemo.git

九、UINavigationController切换视图 实例的更多相关文章

  1. UINavigationController切换视图的简单使用

    UINavigationController通过栈的方式来管理视图,通过push将视图压入栈,pop将视图推出栈. 下面通过简单的示例说明 AppDelegate.m - (BOOL)applicat ...

  2. vuejs切换视图同时保持状态

    vuejs切换视图同时保持状态 http://cn.vuejs.org/guide/components.html#动态组件 动态组件 多个组件可以使用同一个挂载点,然后动态地在它们之间切换.使用保留 ...

  3. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  4. 【Xamarin 开发 IOS --使用 Storyboard Segue 实作 UIViewController 的切换 (实例)】

    注意:在vs2015中进行画板之间的导航的时候,使用CTRL+鼠标左键进行导航的设定. 使用 NavigationController 进行 画板的链接.... 使用 Storyboard Segue ...

  5. Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览

    Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...

  6. viewSwitcher 切换视图

    通过VIewSwitcher切换视图.这个用到了baseAdapter,还是不太懂,先记个笔记. <RelativeLayout xmlns:android="http://schem ...

  7. sql视图实例

    一个视图是一个或一组SQL语句,存储在数据库中相关的名称.一个视图实际上是一个预定义的SQL查询中的表的形式组成. 一个视图可以包含一个表的所有行,或选择表中的行.从一个或多个表上写SQL查询创建一个 ...

  8. angular学习笔记(十七)-路由和切换视图

    本篇介绍angular中如何通过判断url的hash值来显示不同的视图模板,并使用不同的控制器: 下面,通过一个例子,来一步一步说明它的用法: 我们要做一个邮箱应用,打开的时候显示的是邮件列表: 然后 ...

  9. iOS:切换视图时,反向传递数据方法二:代理

    代理: 1.发送信息的控制器设置一个代理,并自定义一个代理的方法,用来传递数据 2.接受信息的控制器遵循发送信息的控制器的协议 3.接受信息的控制器设置发送信息的控制器的代理为自己self 4.接受信 ...

随机推荐

  1. HP SAN Switch參考文檔地址

    HP SAN Switch其實也是基於Linux內核 http://h20566.www2.hpe.com/portal/site/hpsc/template.PAGE/public/psi/manu ...

  2. [Tools]迁移Confluence, JIRA, Fisheye

    [背景] 原先的Confluence, JIRA, Fisheye都部署在一台服务器(192.168.200.203)上,导致这台机器太卡,公司又分配了两台虚拟机来分开这几个应用(192.168.20 ...

  3. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  4. RDS MySQL 全文检索相关问题的处理

    RDS MySQL 全文检索相关问题 1. RDS MySQL 对全文检索的支持 2. RDS MySQL 全文检索相关参数 3. RDS MySQL 全文检索中文支持 3.1 MyISAM 引擎表 ...

  5. Linux系统性能10条命令监控

    Linux系统性能10条命令监控 概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 p ...

  6. SpringMVC解析2-ContextLoaderListener

    对于SpringMVC功能实现的分析,我们首先从web.xml开始,在web.xml文件中我们首先配置的就是ContextLoaderListener,那么它所提供了功能有哪些又是如何实现的?当使用编 ...

  7. hdu1010 dfs+奇偶性减枝

    Tempter of the Bone Problem Description The doggie found a bone in an ancient maze, which fascinated ...

  8. HDU 3068 回文串-Manacher

    题意链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题意:中文题. 思路:Manacher模板题 #include<iostream> ...

  9. 手持终端打印POS机(安装移动销售开单订货会软件)无线传输到订货会后台销售管理系统

    当今的服装市场是品牌竞争时代,产品能否紧随潮流前线并迅速推出市场抢得先机,是品牌成功与否的关键.而订货会是每个鞋服企业新产品走向市场至关重要的开端,订货会如何演绎.成功与否,与品牌在竞争洪流中的命运息 ...

  10. 基于jdk1.7实现的excel导出工具类

    通用excel导出工具类,基于泛型.反射.hashmap 以及基于泛型.反射.bean两种方式 import java.io.*;import java.lang.reflect.Field;impo ...