iOS开发:navigationcontrol…

http://justcoding.iteye.com/blog/1476196
这篇文章介绍使用UINavigationController切换视图。这个NavigationController功能强大,主要用来切换多级的视图。可以将NavigationController理解成一个栈,这个栈中可以存放很多View Controller。在这个栈创建的时候,我们先给它添加一个ViewController,称为Root ViewController,它放在栈底,代表的是刚加载程序的时候显示的视图。当用户新选择了一个想要显示的视图时,那个新的ViewController入栈,它所控制的视图就会显示出来。这个新的View Controller通常称作SubController。

 

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

 

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

我们这次要做的小例子运行如下图:

 

 

左边的图片是刚运行时显示的效果,它是一个表格,表格中的每一行代表不通过的ViewController。注意到每一行的右边有一个图标,叫做DisclosureIndicator,它用来告诉用户单击这一行会进入另一个视图。当点击某行,就进入中间图片显示的视图。中间图片中,左上角的按钮就是NavigationButton。中间的图片中,每一行右边有一个按钮,叫做Detail DisclosureButton,它不仅仅是一个图标,实际上它是一个控件,用户点击它会进入该行的详细说明。点击某行的Detail DisclosureButton,进入相应的视图,如右边的图片。

 

为了更好地理解Navigation Controller的原理,我们从Empty Application开始我们的小例子。

 

1、运行Xcode 4.2,新建一个EmptyApplication,名称为:Navigation Controller Test:

 

 

2、创建一个View Controller,作为RootView Controller:依次选择File——New——New File,在弹出的窗口,左边选择iOS下的CocoaTouch,右边选择UIViewController subclass:

 

 

单击Next,在新窗口输入名称为RootViewController,subof选择UItableViewController:

 

 

之后选好位置,完成创建。

 

3、打开AppDelegate.h,向其中添加属性:


  1. @property (strong, nonatomic) UINavigationController *navController;  

    然后打开AppDelegate.m,在@implementation之前添加代码:


    1. #import "RootViewController.h"  


     

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


    1. @synthesize navController;  
    2.   
    3. #pragma mark   
    4. #pragma mark Application lifecycle  

     

    在didFinishLaunchingWithOptions方法中添加代码:

    C代码    收藏代码
    1. (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2.  
    3.     self.window [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    4.     // Override point for customization after application launch.  
    5.       
    6.     RootViewController *root [[RootViewController alloc] initWithStyle:UITableViewStylePlain];   
    7.     self.navController [[UINavigationController alloc] initWithRootViewController:root];   
    8.     [self.window addSubview:navController.view];  
    9.       
    10.     self.window.backgroundColor [UIColor whiteColor];  
    11.     [self.window makeKeyAndVisible];  
    12.     return YES;  
    13.  

     

    4、我们先要明确,Root ViewController中是一个表格,它的每一行对应一个Sub View Controller。

     

    打开RootViewController.h,添加属性:


    1. @property (strong, nonatomic) NSArray *controllerList;  

     

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


    1. @synthesize controllerList;  

     

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


    1. self.title @"分类"  
    2. NSMutableArray *array [[NSMutableArray alloc] init];   
    3. self.controllerList array;  

     

    在ViewDidUnload方法中添加代码:


    1. self.controllerList nil;  

     

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

    找到numberOfRowsInSection:方法,修改代码为:

    C代码    收藏代码
    1. return [controllerList count];  

     

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

    C代码    收藏代码
    1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    2.  
    3.     static NSString *RootTableViewCell @"RootTableViewCell"  
    4.     UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier: RootTableViewCell];   
    5.     if (cell == nil)   
    6.         cell [[UITableViewCell alloc]   
    7.                 initWithStyle:UITableViewCellStyleDefault  
    8.                 reuseIdentifier: RootTableViewCell];   
    9.       
    10.     NSUInteger row [indexPath row];   
    11.     UITableViewController *controller [controllerList objectAtIndex:row];   
    12.     //这里设置每一行显示的文本为所对应的View Controller的标题  
    13.     cell.textLabel.text controller.title;  
    14.     //accessoryType就表示每行右边的图标  
    15.     cell.accessoryType UITableViewCellAccessoryDisclosureIndicator;   
    16.     return cell;   
    17.  

     

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


    1. (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
    2.  
    3.     NSUInteger row [indexPath row];   
    4.     UITableViewController *nextController [self.controllerList objectAtIndex:row];   
    5.     [self.navigationController pushViewController:nextController animated:YES];   
    6.  

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值