现版本 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. 通过Small Basic把儿子/女儿带入编程的世界

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:今天是儿子3岁的生日,就来介绍一下适合给儿童培养兴趣的编程语言--微软Small Ba ...

  2. 怎样打开64位 Ubuntu 的32位支持功能?

    转自:http://jingyan.baidu.com/article/7082dc1c539c15e40a89bd3e.html 大多数使用基于 Ubuntu/Debian 的发行版的人都更倾向于选 ...

  3. javase基础笔记2——数据类型和面向对象

    API:Application program interface  程序调用一个方法去实现一个功能 正则表达式:regex 用来匹配的 javaEE里边有三大框架 SSH struts spring ...

  4. 两种方法获取shadow ssdt

    ULONG GetShadowSsdtCurrentAddresses( PSSDT_ADDRESS   AddressInfo, PULONG          Length ) { PSYSTEM ...

  5. 如何修改 SQL Server 代理主作业 (Transact-SQL)

    本主题介绍了如何使用存储过程修改 Microsoft SQL Server 代理主作业. 更改作业定义计划的详细信息 1.       执行 sp_update_schedule. 在作业中添加.更改 ...

  6. 【jQuery 区别】.click()和$(document).on("click","指定的元素",function(){});的区别

    给出以下的代码展示: //绑定 下一页 的点击事件 $("a[aria-label='Next']").click(function(){ $("a[aria-label ...

  7. 计数排序-java

    今天看了一本书,书里有道题,题目很常见,排序,明了点说: 需求:输入:最多有n个正整数,每个数都小于n, n为107 ,没有重复的整数 输出:按升序排列 思路:假设有一组集合 {1,3,5,6,11, ...

  8. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  9. CSS3-基于浮动的布局,响应式WEB设计,定位网页上的元素,设计打印页面的css技术

    基于浮动的布局: 1.除非图片设置了宽度,否则始终应该要对浮动的图片设置一个宽度,这样可以让浏览器给其他内容腾出环绕的空间 2.当侧边栏的高度与主内容区的高度不一致的时候,可以用个margin进行调整 ...

  10. Android定位方式和测试方法

    Android常用的三种定位方式有:基于GPS定位.基于基站地位.基于wifi定位. 1.基于GPS定位: GPS定位需要GPS模块(硬件)的支持,没有GPS模块是无法进行GPS定位的. GPS定位最 ...