UINavigationController详解(一)

最后更新于:2022-04-01 06:58:59

@UINavigationControlle简介: 1.导航控制器,专门管理具有层级关系内容的导航 2.采用栈的方式管理所有controller,每个controller管理各自的视图 3.推出显示一个新的controller 4.提供返回上一级controller的默认button和方法 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-12_5694d75262976.jpg) @UINavigationControlle结构: 1.它是UIViewController的子类,因此它也由view属性 2.它的view包含3个子视图:navigationBar     contentView(不是属性)      toolbar 3.导航控制器使用这些对象实现导航界面,我们负责提供这些对象,进行展示,参考下图(苹果官方): ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-12_5694d752881d0.jpg)                         UINavigationController view层级 @UINavigationControlle的创建: 1.它以栈的方式管理所控制的视图,至少要有一个被管理的UIViewController作为rootViewController 2.导航界面的对象会被同时创建 3.设置delegate,监控导航堆栈的变化,即viewControllers,topController,visibleController等等 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-12_5694d752a57e0.jpg) ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-12_5694d752c201c.jpg) 代码示例: ~~~ HMTViewController * hmtView = [[HMTViewController alloc]init]; UINavigationController * rootNavigation = [[UINavigationController alloc]initWithRootViewController:hmtView]; NSLog(@"%@",rootNavigation); self.window.rootViewController = rootNavigation; ~~~ @UINavigationControlle导航方法: 1.推出下一级controller(入栈):     pushViewController:animated: 2.返回上一级controller(出栈):     popViewControllerAnimated:                         // 返回上一级      popToRootViewControllerAnimated:              // 返回根视图(中间无论多少个视图都全部被出栈,没了)     // *@property**(**nonatomic**,**copy**)**NSArray** *viewControllers;**// The current view controller stack.*     // 通过*[**self**.**navigationController**.**viewControllers **objectAtIndex**:**n**] 来选定指定视图*     popToViewController:animated:                     //  返回指定视图 3.导航控制器提供已有的控件实现返回上一级(即被根视图推出的View都自带一个返回 代码示例: @这里必须要说一下self.navigationController   // If this view controller has been pushed onto a navigation controller, return it. ~~~ - (void)viewDidLoad { [super viewDidLoad]; UIButton *push = [UIButton buttonWithType:UIButtonTypeSystem]; push.frame =CGRectMake(100, 100, 200, 200); [push setTitle:@"下一页" forState:UIControlStateNormal]; [push addTarget:self action:@selector(pushNextController) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:push]; // Do any additional setup after loading the view. } - (void)pushNextController{ NSLog(@"%@",self.navigationController); HMTNext2ViewController * hmtNext2 = [[HMTNext2ViewController alloc]init]; [self.navigationController pushViewController:hmtNext2 animated:YES]; } ~~~
';