码迷,mamicode.com
首页 > 其他好文 > 详细

导航控制器的制作,push,pop,root,index

时间:2014-10-30 10:26:17      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:uialertview   push   导航控制器   pop   root   

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

标签:uialertview   push   导航控制器   pop   root   

原文地址:http://blog.csdn.net/pengyuan_d/article/details/40614425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!