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. 强联通块tarjan算法

    http://poj.org/problem?id=1236第一问:需要几个学校存在软件,才能通过传递,使得所有的学校都有软件 用tarjan算法求出强联通分量后,将每个联通分量缩成一个点,那么问题1 ...

  2. 高仿快车100--实战RadioGroup和RadioButton应用

    1.RadioButtonCheckBox的差别: a.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后.通过点击能够变为未选中 b.一组RadioButton ...

  3. accept功能

    accept()功能 系统调用 accept() 这将是一个有点陌生的地方! 你可以想象发生 这种事情:这是非常远离你通过倾听 (listen()) 的port连接 (connect()) 你的机器. ...

  4. A. Initial Bet(Codeforces Round #273)

    A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. 採訪The Molasses Flood:BioShock Infinite 游戏之后又一大作

    Xsolla有幸与Flame in the Flood游戏的开发人员之中的一个-----Forrest Dowling进行了採訪,Flame in the Flood这款非常棒的游戏在Kickstar ...

  6. MVC 中使用 SignalR 实现推送功能

    MVC 中使用 SignalR 实现推送功能 一,简介 Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Pus ...

  7. 表的顺序结构---重写Arraylist类

    重写ArrayList类,为防止冲突,重写为MyArrayList,未继承Iterable类. public class MyArrayList<AnyType>{ int N=10; A ...

  8. javascript中的三角学

    三角学主要研究三角形和它们的边角关系,包含一个90度角的三角形被称为直角三角形.在这里主要研究直角三角形相关的知识. 1. 角度和弧度 360(角度) = 2*Math.PI(弧度) degrees ...

  9. Mac OS X Yosemite安装Hadoop 2.6记录

    整个安装过程分为四部分: 一.  安装Homebrew 二.  ssh localhost 三. 安装Hadoop已经进行配置文件设置 (伪分布式) 四. 执行栗子 一. 安装Homebrew 採用H ...

  10. Jafka来源分析——Processor

    Jafka Acceptor接受client而建立后的连接请求,Acceptor会将Socket连接交给Processor进行处理.Processor通过下面的处理步骤进行client请求的处理: 1 ...