九、UINavigationController切换视图 实例
现版本 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切换视图 实例的更多相关文章
- UINavigationController切换视图的简单使用
UINavigationController通过栈的方式来管理视图,通过push将视图压入栈,pop将视图推出栈. 下面通过简单的示例说明 AppDelegate.m - (BOOL)applicat ...
- vuejs切换视图同时保持状态
vuejs切换视图同时保持状态 http://cn.vuejs.org/guide/components.html#动态组件 动态组件 多个组件可以使用同一个挂载点,然后动态地在它们之间切换.使用保留 ...
- iOS开发:使用Tab Bar切换视图
iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...
- 【Xamarin 开发 IOS --使用 Storyboard Segue 实作 UIViewController 的切换 (实例)】
注意:在vs2015中进行画板之间的导航的时候,使用CTRL+鼠标左键进行导航的设定. 使用 NavigationController 进行 画板的链接.... 使用 Storyboard Segue ...
- Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
Android技术--切换视图(一)~(四)在资源项目:https://github.com/YongYuIT/MeiNv_Liulanqi 一.早期android(android.support.v ...
- viewSwitcher 切换视图
通过VIewSwitcher切换视图.这个用到了baseAdapter,还是不太懂,先记个笔记. <RelativeLayout xmlns:android="http://schem ...
- sql视图实例
一个视图是一个或一组SQL语句,存储在数据库中相关的名称.一个视图实际上是一个预定义的SQL查询中的表的形式组成. 一个视图可以包含一个表的所有行,或选择表中的行.从一个或多个表上写SQL查询创建一个 ...
- angular学习笔记(十七)-路由和切换视图
本篇介绍angular中如何通过判断url的hash值来显示不同的视图模板,并使用不同的控制器: 下面,通过一个例子,来一步一步说明它的用法: 我们要做一个邮箱应用,打开的时候显示的是邮件列表: 然后 ...
- iOS:切换视图时,反向传递数据方法二:代理
代理: 1.发送信息的控制器设置一个代理,并自定义一个代理的方法,用来传递数据 2.接受信息的控制器遵循发送信息的控制器的协议 3.接受信息的控制器设置发送信息的控制器的代理为自己self 4.接受信 ...
随机推荐
- Centos6.5里安装Erlang 并安装riak
一.Erlang安装: 1 首先进入www.erlang.org 下载页面,下载otp_src_17.5.tar.gz. IT网,http://www.it.net.cn 2 解压缩:tar -xzv ...
- Oracle11g在使用exp导出时不导出空表问题的解决办法
11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...
- V for Vendetta
V for Vendetta V字仇杀队 复仇者V 安迪·沃卓斯基 and Larry Wachowski 思想,是最强大的武器.因为,世界上的独裁政府,有一个共同特点就是推行洗脑和愚民政策. 经典台 ...
- Android studio导入eclipse项目且不改变目录结构
Android studio的安装与配置论坛当中已经有很多在此就不在细说了,现在开始说下如何在Android studio当中导入eclipse的项目且不改变其目录结构和配置,让使用eclipse的同 ...
- 【HTML】HTML特殊符号【转http://www.cnblogs.com/web-d/archive/2010/04/16/1713298.html】
HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ...
- python 定义类方法
定义类方法 和属性类似,方法也分实例方法和类方法. 在class中定义的全部是实例方法,实例方法第一个参数 self 是实例本身. 要在class中定义类方法,需要这么写: class Person( ...
- java 大数计算
这几天做了几道用大数的题,发现java来做大数运算十分方便.对acmer来说是十分实用的 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigInteger ...
- 【JAVA】数字相加
设计思想:定义双精度变量 i 和 s=0 ,定义另一个变量 n 作为计数,方便之后求平均值.程序里将数据输入 i 中,通过 s 来将各个数据求和,最后除 n 得平均值. 程序流程图: 源程序代码: p ...
- Linux IO模型和网络编程模型
术语概念描述: IO有内存IO.网络IO和磁盘IO三种,通常我们说的IO指的是后两者. 阻塞和非阻塞,是函数/方法的实现方式,即在数据就绪之前是立刻返回还是等待. 以文件IO为例,一个IO读过程是文件 ...
- Swift3.0语言教程使用URL字符串
Swift3.0语言教程使用URL字符串 Swift3.0语言教程使用URL字符串,和路径一样,URL其实也是字符串,我们可以将这些字符串称为URL字符串.本小节将讲解URL字符串的使用. 1.编码 ...