IOS学习 NSNavigationController 多个子页面间相互跳转

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    self.homeVC = [[HomeViewController alloc]init];

    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.homeVC];

    self.window.rootViewController = navigation;

    self.window.backgroundColor = [UIColor purpleColor];


    return YES;

}



@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];

    button.layer.cornerRadius = 8;

    button.backgroundColor = [UIColor greenColor];

    [button setTitle:@"push" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

}


-(void)pushVC{

    UIViewController *secondVC = [[SecondViewController alloc]init];

    secondVC.view.backgroundColor = [UIColor cyanColor];

    //跳转至子页面

    [self.navigationController pushViewController:secondVC animated:YES];


}



@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];

    button.layer.cornerRadius = 8;

    button.backgroundColor = [UIColor purpleColor];

    [button setTitle:@"second" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(backVC) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

    UIButton *button2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 200, 40)];

    button2.layer.cornerRadius = 8;

    button2.backgroundColor = [UIColor orangeColor];

    [button2 setTitle:@"push" forState:UIControlStateNormal];

    [button2 addTarget:self action:@selector(pushVC3) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];    

}


-(void)backVC{

    //导航栏隐藏

    if (self.navigationController.toolbarHidden) {

        [self.navigationController setToolbarHidden:NO animated:YES];

        [self.navigationController setNavigationBarHidden:NO animated:YES];

    }else

    {

        [self.navigationController setToolbarHidden:YES animated:YES];

        [self.navigationController setNavigationBarHidden:YES animated:YES];

    }

}


-(void)pushVC3{

    UIViewController *threeVC = [[ThreeViewController alloc]init];

    threeVC.view.backgroundColor = [UIColor yellowColor];

    //跳转至子页面

    [self.navigationController pushViewController:threeVC animated:YES];    

}



@implementation ThreeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    NSArray *array = @[@"push",@"pop",@"root",@"index"];

    for (int i=0; i<4; i++) {

        

        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100*(i+1), 200, 40)];

        button.layer.cornerRadius = 8;

        button.backgroundColor = [UIColor redColor];

        NSString *str = array[i];

        button.tag = i+1;

        [button setTitle:str forState:UIControlStateNormal];

        [button addTarget:self action:@selector(backVC:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

    }

}


- (void)backVC:(UIButton *)sender{

    switch (sender.tag) {

        case 1:

            //push 至子页面

            threeVC.view.backgroundColor = [UIColor yellowColor];

            threeVC = [[ThreeViewController alloc]init];

            [self.navigationController pushViewController:threeVC animated:YES];

            break;

        case 2:

            //pop 返回至上一页面

            [self.navigationController popViewControllerAnimated:YES];

            break;

        case 3:

            //popToRoot 返回至根页面

            [self.navigationController popToRootViewControllerAnimated:YES];

            break;

        case 4:

            //popToView 返回至指定页面

            secondVC = [[self.navigationController viewControllers]objectAtIndex:1];

            [self.navigationController popToViewController:secondVC animated:YES];

            break;

        default:

            break;

    }

}




你可能感兴趣的:(学习)