导航控制器生产,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]; } } }
版权声明:本文博主原创文章。博客,未经同意不得转载。