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. bnu1066

    hnu1066 给我们一张图,问我们摧毁边使得s和t不连通有多少种方案, 方案与方案之间不能存在相同的摧毁目标. 这是一个神奇的题目. 这题可以转为求s与t的最短路,为什么呢? 因为方案与方案之间不能 ...

  2. volatile解析(转)

    Java并发编程:volatile关键字解析 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...

  3. Hadoop学习笔记(两)设置单节点集群

    本文描写叙述怎样设置一个单一节点的 Hadoop 安装.以便您能够高速运行简单的操作,使用 Hadoop MapReduce 和 Hadoop 分布式文件系统 (HDFS). 參考官方文档:Hadoo ...

  4. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  5. JavaScript权威指南科03章 种类、值和变量(1)

    种类.值和变量 数据类型分类: 基本类型(primitive type):数位 弦 布尔值 null undefined 对象类型(object type): 对象是属性的集合,每一个属性都由&quo ...

  6. 阅读:AirBag Boosting Smartphone Resistance to Malware Infection

  7. 冒泡排序算法 C++和PHP达到

    冒泡排序是小元素向前或向后的大要素.两个相邻元件之间的比较结果更.交换也这两个元件之间发生.它是最慢的排序算法. 效率最低的算法. 时间复杂度: 它是最差时间复杂度为:O(n^2),冒泡排序最好的时间 ...

  8. delphi webbrowser 经常使用的演示样本

    var Form : IHTMLFormElement ; D:IHTMLDocument2 ; begin with WebBrowser1 do begin D := Document as IH ...

  9. response.setHeader各种使用方法

    一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其它页面 response.setHeader("re ...

  10. DV工作流