九、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.接受信 ...
随机推荐
- c++ 调用dll
1.首先写一个dll程序并且输出成dll. 新建win32项目,然后在应用程序类型中选择dll. HelloDll.h: #pragma once #ifndef MYDLL_API_EXPORTS ...
- hdu 3709 数位dp
数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...
- 数据库是.frm,.myd,myi备份如何导入mysql (转)
今天找了个案例,琢磨了半天,才分析大概出来,数据库是.frm,.myd,myi备份,不会导入mysql,到网上找了些资料,导入成功. 首先说一下这几种文件是干什么的,*.frm是描述了表的结构,*.M ...
- C# 获取wave文件信息【转】
public class WaveHelper { /// <summary> /// 数据流 /// </summary> private Stream m_WaveData ...
- 在Salesforce中处理Xml的生成与解析
在Salesforce中处理Xml的生成与解析 1): Generate Xml private String ConvertAccountToXmlInfo(Account acc){ Dom.Do ...
- Arduino101学习(一)——Windows下环境配置
一.Arduino IDE下载 要开发Arduino 101/Genuino 101,你需要先安装并配置好相应的开发环境.下载地址 http://www.arduino.cn/thread-5838- ...
- 【转】Spark-Sql版本升级对应的新特性汇总
Spark-Sql版本升级对应的新特性汇总 SparkSQL的前身是Shark.由于Shark自身的不完善,2014年6月1日Reynold Xin宣布:停止对Shark的开发.SparkSQL抛弃原 ...
- 系统表达式(z变换、DTFT、差分方程)之间的关系
- 从新注册 .DLL CMD 运行regsvr32 *.dll注册该DLL 或 regsvr32 /s *.DLL 求证
从新注册 .DLL CMD 运行regsvr32 *.dll注册该DLL 或 regsvr32 /s *.DLL 求证
- 【bzoj3624】【apio2008】免费道路
2016/06/25 诸老师讲的图论,听了这道题很想写一下,但是看来要留到期末考后了. 07/01 有的标记是说生成树,有的是并查集...然而我只是觉得这棵奇怪的生成树蛮精妙的... 题目比较难过的只 ...