导航控制器生产,push,pop,root,index
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的更多相关文章
- iOS开发——UI进阶篇(十)导航控制器、微博详情页、控制器的View的生命周期
一.导航控制器出栈 1.initWithRootViewController本质 UIViewController *vc = [[OneViewController alloc] init]; // ...
- 【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的加入删除、数据存储等
(1)效果 (2)源码与第三方类库下载 http://download.csdn.net/detail/wsb200514/8155979 (3)总结 --导航控制器,能够直接用代码的push和pop ...
- iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用
一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...
- UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解
(1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...
- iOS 导航控制器如何随意push和pop 想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop 的A
这里主要是对导航控制器的viewControllerss这个数组进行操作,因为push操作和pop操作都是根据这个数据去切换控制器或者在这个数组里增加控制器的,所以只要改变这个子控制器数据就能自定义切 ...
- iOS开发-21UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解
(1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...
- 【iOS开发-21】UINavigationController导航控制器初始化,导航控制器栈的push和pop跳转理解
(1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最以下,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...
- 截获导航控制器系统返回按钮的点击pop及右滑pop事件
前几天看了@栾小布的一篇文章:Custom backBarButtonItem,在跟着做的时候我又顺便扩展了一些,写此文章的目的是为了总结一下自己所写的东西,方便以后翻看容易,同时也是自己入行iOS一 ...
- iOS正确解决隐藏导航栏后push和pop或dismiss和present闪黑问题
情景: 一级页面不显示导航栏 ,二级页面显示导航栏. 方法一 适用于push/pop: 一级页面中 - (void)viewWillAppear:(BOOL)animated { [super vie ...
随机推荐
- Windows Phone开发(24):启动器与选择器之发送短信
原文:Windows Phone开发(24):启动器与选择器之发送短信 本节我们通过一个简单的发送短信示例来演示一下如果配合使用PhoneNumberChooserTask和SmsComposeTas ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- vs修错小知识,相当于我自己的笔记,需要的人可以看下
在出现这种外部错误时,首先得检查你自己的的类的方法是否都以及实现了,我就是出现没有实现某些方法所以报出这个错误!!! 结果是onExit()方法我定义了没有去实现它. (待续)
- Android:简单的弹幕效果达到
首先,效果图.分类似至360检测到的骚扰电话页面: 布局非常easy,上面是一个RelativeLayout,以下一个Button. 功能: (1)弹幕生成后自己主动从右側往左側滚动(Translat ...
- How to fix Column 'InvariantName' is constrained to be unique 解决办法!
Introduction When you build a web project that uses Enterprise Library Community for the Application ...
- android得知----overridePendingTransition
1 Activity动画是指从一个切换activity跳到另一个activity随着电影. 它由两部分组成:第一部分是一个activity动画出口:中的第二个另一部分activity动画被访问: 于A ...
- Android - 数据存储 -在SQL数据库中保存数据
对于重复的或结构化的数据,保存到数据库中是很好的选择,比如联系人信息.这里假设你对SQL数据库大体上了解然后帮助你学习Android上的SQLite数据库.在Android数据库上需要用到的API可以 ...
- 开源Math.NET基础数学类库使用(05)C#解析Delimited Formats数据格式
原文:[原创]开源Math.NET基础数学类库使用(05)C#解析Delimited Formats数据格式 开源Math.NET基础数学类库使用系列文章总目录: 1.开源.NET基础数学计算组件 ...
- IP多播(组播)
IP多播是实现数据一对多通信的模式.从一个源点传送到多个目的地,数据仅仅拷贝一份.这里说的数据仅仅拷贝一份,是指在每一条须要它的两个点之间,数据仅仅有一份.例如以下图为<计算机网络>(谢希 ...
- python基础课程_学习笔记26:编程的乐趣
编程的乐趣 编程柔术 当你坐下来,打算如何组织计划要定时,具体程序,然而,无论什么经验.在实现时间的函数的,你会逐渐学会了原来的设计,实用的新知识.我们不应该忽视沿途汲取的教训,相反,它们用于其他设计 ...