UINavigationController

使用UINavigationController主要用来切换多级视图,切换视图就是View Controller的切换。


将UINavigationController理解成一个栈,栈中存放很多View Controller。

这个栈创建的时候,先给它添加一个View Controller,称为Root View Controller,它放在栈底,代表刚加载程序的时候显示的视图。

当用户新选择了一个想要显示的视图时,新的View Controller入栈,它所控制的视图就会显示出来。这个新的View Controller通常称作Sub Controller。

进入新的视图后,左上方出现一个按钮,叫做Navigation Button,就像浏览器的后退按钮一样,点击此按钮,当前的View Controller出栈,之前的View就会显示。


这种设计模式使得开发变得简单,我们只需知道每一个View Controller的Sub Controller就好了。


表格中的每一行代表不同的View Controller。

每一行右边有一个图标,叫做DisclosureIndicator,用来告诉用户单击这一行会进入另一个视图。

每一行右边有一个按钮,叫做Detail Disclosure Button,它不仅仅是一个图标,实际上它是一个控件,用户点击它会进入该行的详细说明。

点击某行的Detail Disclosure Button,进入相应的视图。


实际使用:


1、创建 Table View Controller,作为Root View Controller:


2、AppDelegate.h 中添加属性:

@property (strong, nonatomic) UINavigationController *navController;


AppDelegate.m 中,


在@implementation之前添加代码:

#import "RootViewController.h"


在@synthesizewindow = _window;之后添加代码:

@synthesize navController;


 在didFinishLaunchingWithOptions方法中添加代码:

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

{

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

   

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

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

    [self.windowaddSubview:navController.view];

   

   self.window.backgroundColor = [UIColor whiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}


3、Root View Controller 是一个表格,每一行对应一个Sub Controller。


4、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;


在ViewDidUnload方法中添加代码:

self.controllerList = nil;


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


numberOfRowsInSection方法,修改代码为:

return [controllerList count];


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

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(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 *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUInteger row= [indexPath row];

   UITableViewController *nextController = [self.controllerListobjectAtIndex:row];

   [self.navigationController pushViewController:nextControlleranimated:YES];

}


5、现在还看不到效果,那是因为controllerList这个数组现在是空的。

创建Table View Controller,用于显示电影列表,名称为:MovieViewController。

创建View Controller,名称为MovieDetailViewController,用于显示电影的详细信息,选中include xib file。


6、打开MovieDetailViewController.xib,拖一个Label到中间。将其映射到MovieDetailViewController.h中,名称为detailLabel:


在MovieDetailViewController.h中添加属性:

@property (copy, nonatomic) NSString *message;


MovieDetailViewController.m 中,


在@implementation之后添加代码:

@synthesize message;


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

- (void)viewWillAppear:(BOOL)animated {

   detailLabel.text = message;

    [superviewWillAppear:animated];

}


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


在viewDidUnload方法中添加代码:

self.detailLabel = nil;

self.message = nil;


7、在MovieViewController.h 中添加属性:

@property (strong, nonatomic) NSArray *movieList;

@property (strong, nonatomic) MovieDetailViewController*childController;

在@interface之前添加代码:

#import "MovieDetailViewController.h"

#import "AppDelegate.h"


MovieViewController.m 中,


在@implementation之后添加代码:

@synthesize movieList;

@synthesize childController;


在viewDidLoad方法中添加代码:

NSArray *array = [[NSArray alloc] initWithObjects:@"肖申克的救赎", @"教父",@"教父:II",@"低俗小说", @"黄金三镖客", @"十二怒汉", @"辛德勒名单",@"蝙蝠侠前传2:黑暗骑士",@"指环王:王者归来", @"飞越疯人院",@"星球大战Ⅴ:帝国反击战",@"搏击俱乐部", @"盗梦空间",@"七武士",@"指环王:护戒使者", @"好家伙", @"星球大战IV:新希望", @"上帝之城",@"卡萨布兰卡", @"黑客帝国", @"西部往事", @"后窗", @"夺宝奇兵",@"沉默的羔羊", @"非常嫌疑犯", @"七宗罪", @"指环王:双塔奇兵", @"阿甘正传",@"惊魂记", @"美好人生", nil];

self.movieList = array;


在ViewDidUnload方法中添加代码:

self.movieList = nil;

self.childController = nil;


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


numberOfRowsInSection方法,修改代码为:

return [movieList count];


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

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(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 *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

}


在@end之前添加方法:

- (void)tableView:(UITableView *)tableView

 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

    if(childController == nil) {

       childController = [[MovieDetailViewController alloc]

                          initWithNibName:@"MovieDetailViewController" bundle:nil];

    }

    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];

}


8、RootViewController.m 中,


在@implementation之前添加代码:

#import "MovieViewController.h"


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

//电影

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

movieViewController.title = @"电影"; 

[array addObject:movieViewController];


9、运行一下:


RootViewController表格中其他选项的实现跟上面是类似的,重复操作比较多,不再讲了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值