导航控制器的制作,push,pop,root,index
2014-10-30 09:23
267 查看
AppDelegate.m
FirstViewController.m
SecondViewController.m
//弹出到指定的控制器// [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]; } } }
#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中用在导航控制器的转场动画(pop,push动画)制作
- iOS开发UINavigationController导航控制器初始化,导航控制器栈的push和pop跳转理解
- iOS开发-21UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解
- iOS开发-21UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解
- 导航控制器push/pop
- iOS开发-21UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解
- ios导航控制器UINavigationController,控制器a跳转(push)到b后,b跳转(push)到c,但c后退(pop)进入a
- 重写导航控制器,利用重写pop和push方法来隐藏底层的tabbar和其他一些事件
- UINavigationController导航控制器弹出(pop)和压入(push)的顺序问题
- iOS 导航控制器如何随意push和pop 想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop 的A
- 【iOS开发-21】UINavigationController导航控制器初始化,导航控制器栈的push和pop跳转理解
- XMG 导航控制器 设置RootViewController也会去调用push方法
- 在导航控制器的rootviewcontroller中push一个控制器 出现view重叠现象
- 【iOS开发-21】UINavigationController导航控制器初始化,导航控制器栈的push和pop跳转理解
- 自己遇到的 导航控制器 push了好几层 让他一次返回到前面的指定页面 不一定是根页面
- A、B、C、D 四个视图控制器想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop 的A
- 自定义控制器的转场动画(Push、Pop)
- 控制器转场动画自定义(1):push/pop的实现
- iOS实现先无动画pop再push,替换当前控制器