AppDelegate.m

#import "FirstViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; //创建视图控制器
FirstViewController *firstCtrl = [[FirstViewController alloc] init]; //创建导航控制器
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:firstCtrl]; self.window.rootViewController = navCtrl; return YES;
}

FirstViewController.m

- (void)viewDidLoad
{
[super viewDidLoad]; //设置标题
self.title = @"第一个控制器"; self.view.backgroundColor = [UIColor grayColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 40);
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"push" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; //取得当前视图控制器的导航栏
UINavigationBar *navBar = self.navigationController.navigationBar;
//获取导航向
NSArray *items = navBar.items;
NSLog(@"firstItems:%@",items);
NSLog(@"fiestBar:%@",navBar); } - (void)buttonAction:(UIButton *)button
{ SecondViewController *secondCtrl = [[SecondViewController alloc] init];
//导航到下一个视图控制器
[self.navigationController pushViewController:secondCtrl animated:YES]; }

SecondViewController.m

- (void)viewDidLoad
{
[super viewDidLoad]; self.title = @"第二个控制器"; self.view.backgroundColor = [UIColor orangeColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 40);
button.tag = 101;
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"push" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 102;
button1.frame = CGRectMake(100, 180, 100, 40);
button1.backgroundColor = [UIColor greenColor];
[button1 setTitle:@"pop" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1]; //打印的结果是当前对象secondctrl
// NSLog(@"%@",self.navigationController.topViewController);
// NSLog(@"%@",self.navigationController.visibleViewController); //返回导航控制器的自控制器的个数
/*
"<FirstViewController: 0x93328c0>",
"<SecondViewController: 0x8f1ca00>"
*/
NSLog(@"%@",self.navigationController.viewControllers); } - (void)viewDidAppear:(BOOL)animated { //取得当前视图控制器的导航栏
UINavigationBar *navBar = self.navigationController.navigationBar; // [navBar pushNavigationItem:<#(UINavigationItem *)#> animated:<#(BOOL)#>] //获取导航向
NSArray *items = navBar.items;
NSLog(@"secondItems:%@",items);
NSLog(@"secondBar:%@",navBar); [super viewDidAppear:animated]; } - (void)buttonAction:(UIButton *)button
{
if (button.tag == 101) {
ThirdViewController *thirdCtrl = [[ThirdViewController alloc] init];
//导航到下一个视图控制器
[self.navigationController pushViewController:thirdCtrl animated:YES];
}else if (button.tag == 102) { [self.navigationController popViewControllerAnimated:YES];
}
}

ThirdViewController.h

@interface ThirdViewController : UIViewController<UIAlertViewDelegate>

ThirdViewController.m

- (void)viewDidLoad
{
[super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; //导航控制器子控制器的数组
NSArray *viewCtrls = self.navigationController.viewControllers; self.title = [NSString stringWithFormat:@"第%d个控制器",viewCtrls.count]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 40);
button.tag = 101;
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"push" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 102;
button1.frame = CGRectMake(100, 180, 100, 40);
button1.backgroundColor = [UIColor greenColor];
[button1 setTitle:@"pop" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1]; UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag = 103;
button2.frame = CGRectMake(100, 260, 100, 40);
button2.backgroundColor = [UIColor greenColor];
[button2 setTitle:@"root" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2]; UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button3.tag = 104;
button3.frame = CGRectMake(100, 340, 100, 40);
button3.backgroundColor = [UIColor greenColor];
[button3 setTitle:@"index" forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3]; } - (void)buttonAction:(UIButton *)button { if (button.tag == 101) {
<span style="color:#ff0000;"> //push</span>
ThirdViewController *thirdCtrl = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdCtrl animated:YES];
}else if (button.tag == 102) { <span style="color:#ff0000;"> //pop</span>
[self.navigationController popViewControllerAnimated:YES]; }else if (button.tag == 103) { <span style="color:#ff0000;"> //root</span>
[self.navigationController popToRootViewControllerAnimated:YES]; }else if (button.tag == 104) {
<span style="white-space:pre"> </span><pre name="code" class="objc"><span style="color:#ff0000;"><span style="white-space:pre"> </span>//index跳到指定控制器</span>

UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"输入提示" message:@"请输入须要跳转的界面" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; //设置样式 alerView.alertViewStyle = UIAlertViewStylePlainTextInput; [alerView show];
//弹出到指定的控制器// [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>] } }#pragma mark-- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { //获取输入的内容 UITextField
*field = [alertView textFieldAtIndex:0]; NSString *text = field.text; if (text.length != 0) { //将字符串转换成数字 int num = [text intValue]; //推断输入是否合法 if (num >= self.navigationController.viewControllers.count || num<0) { return; } //取得响应的视图控制器 UIViewController *viewCtrl
= [self.navigationController.viewControllers objectAtIndex:num-1]; [self.navigationController popToViewController:viewCtrl animated:YES]; } } }



版权声明:本文博主原创文章。博客,未经同意不得转载。

导航控制器生产,push,pop,root,index的更多相关文章

  1. iOS开发——UI进阶篇(十)导航控制器、微博详情页、控制器的View的生命周期

    一.导航控制器出栈 1.initWithRootViewController本质 UIViewController *vc = [[OneViewController alloc] init]; // ...

  2. 【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的加入删除、数据存储等

    (1)效果 (2)源码与第三方类库下载 http://download.csdn.net/detail/wsb200514/8155979 (3)总结 --导航控制器,能够直接用代码的push和pop ...

  3. iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用

    一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...

  4. UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

  5. iOS 导航控制器如何随意push和pop 想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop 的A

    这里主要是对导航控制器的viewControllerss这个数组进行操作,因为push操作和pop操作都是根据这个数据去切换控制器或者在这个数组里增加控制器的,所以只要改变这个子控制器数据就能自定义切 ...

  6. iOS开发-21UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

  7. 【iOS开发-21】UINavigationController导航控制器初始化,导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最以下,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

  8. 截获导航控制器系统返回按钮的点击pop及右滑pop事件

    前几天看了@栾小布的一篇文章:Custom backBarButtonItem,在跟着做的时候我又顺便扩展了一些,写此文章的目的是为了总结一下自己所写的东西,方便以后翻看容易,同时也是自己入行iOS一 ...

  9. iOS正确解决隐藏导航栏后push和pop或dismiss和present闪黑问题

    情景: 一级页面不显示导航栏 ,二级页面显示导航栏. 方法一 适用于push/pop: 一级页面中 - (void)viewWillAppear:(BOOL)animated { [super vie ...

随机推荐

  1. 【C语言的日常实践(十二)】命令行参数

    C计划main函数有两个参数.文章1一个通常被称为argc,它代表的命令行参数的个数. 第2个通常称为argv.它指向一组參数值. 指针数组:这个数组的每一个元素都是一个字符指针,数组的末尾是一个NU ...

  2. spring 重定向以及转发 乱码问题解决

    1.spring 转发 request.setAttribute("id", id); request.setAttribute("name",name); r ...

  3. web:转盘抽奖

    移动web:转盘抽奖(幸运大转盘)   为了获取客户.回馈客户,平台一般会推出抽奖活动类的营销页.因此web页面中,有各式各样的抽奖效果. 格子式(九宫格),背景滚动式(数字/文字/图案),旋转式(转 ...

  4. textarea文本字段的宽度和高度(width、height)自己主动适应不断变化的处理

    来源:http://www.cnblogs.com/jice/archive/2011/08/07/2130069.html <HTML> <HEAD> <TITLE&g ...

  5. Tomcat部署发布JSP应用程序的三种方法 (转)

    Tomcat部署发布JSP应用程序的三种方法     1.直接放到Webapps目录下      Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应 ...

  6. Andoird实现类似iphone AssistiveTouch的控件的demo

    类似Iphone Assistive Touch的控件的实现 网上也有些这方面的控件,不过貌似不怎么好用,或者是论坛需要积分下载,恰好自己在项目中有用到这种控件,就打算自己写一个,也成功实现了这种功能 ...

  7. Android UI设计规则

    Android UI技巧 1.1 不该做什么 l  不要照搬你在其他平台的UI设计,应该让用户使用感觉是在真正使用一个Android软件,在你的LOGO显示和平台总体观感之间做好平衡 l  不要过度使 ...

  8. MongoDB:逐渐变得无关紧要

    我与MongoDB的关系可分为三个阶段.对于目前处于第三阶段的我来说,这款产品似乎变得无关紧要了.很快你就会明白为什么我这么说. 阶段一:痴迷 我与MongoDB的第一次接触十分神奇:一个poligl ...

  9. 最大公约数(Greatest Common Divisor)

    两个数的最大公约数.一个典型的解决方案是欧几里德,叫欧几里德算法. 原理:(m,n)代表m和nGCD,和m>n.然后,(m,n)=(n,m%n)=.....直到余数为0. 码如下面: publi ...

  10. 好的安排小明(南阳19)(DFS)

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 小明十分聪明,并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...