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

来源:互联网 发布:淘宝v达人 编辑:程序博客网 时间:2024/05/19 16:50

@implementation AppDelegate


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

    // Override point for customization after application launch.

    

    self.homeVC = [[HomeViewControlleralloc]init];

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

    self.window.rootViewController = navigation;

    self.window.backgroundColor = [UIColorpurpleColor];


    return YES;

}



@implementation HomeViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

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

    button.layer.cornerRadius =8;

    button.backgroundColor = [UIColorgreenColor];

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

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

    [self.viewaddSubview:button];

    

}


-(void)pushVC{

    UIViewController *secondVC = [[SecondViewControlleralloc]init];

    secondVC.view.backgroundColor = [UIColorcyanColor];

    //跳转至子页面

    [self.navigationControllerpushViewController:secondVC animated:YES];


}



@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

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

    button.layer.cornerRadius =8;

    button.backgroundColor = [UIColorpurpleColor];

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

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

    [self.viewaddSubview:button];

    

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

    button2.layer.cornerRadius =8;

    button2.backgroundColor = [UIColororangeColor];

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

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

    [self.viewaddSubview:button2];    

}


-(void)backVC{

    //导航栏隐藏

    if (self.navigationController.toolbarHidden) {

        [self.navigationControllersetToolbarHidden:NOanimated:YES];

        [self.navigationControllersetNavigationBarHidden:NOanimated:YES];

    }else

    {

        [self.navigationControllersetToolbarHidden:YESanimated:YES];

        [self.navigationControllersetNavigationBarHidden:YESanimated:YES];

    }

}


-(void)pushVC3{

    UIViewController *threeVC = [[ThreeViewControlleralloc]init];

    threeVC.view.backgroundColor = [UIColoryellowColor];

    //跳转至子页面

    [self.navigationControllerpushViewController:threeVC animated:YES];    

}



@implementation ThreeViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

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

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

        

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

        button.layer.cornerRadius =8;

        button.backgroundColor = [UIColorredColor];

        NSString *str = array[i];

        button.tag = i+1;

        [button setTitle:strforState:UIControlStateNormal];

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

        [self.viewaddSubview:button];

    }

}


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

    switch (sender.tag) {

        case 1:

            //push 至子页面

            threeVC.view.backgroundColor = [UIColoryellowColor];

            threeVC = [[ThreeViewControlleralloc]init];

            [self.navigationControllerpushViewController:threeVCanimated:YES];

            break;

        case 2:

            //pop 返回至上一页面

            [self.navigationControllerpopViewControllerAnimated:YES];

            break;

        case 3:

            //popToRoot 返回至根页面

            [self.navigationControllerpopToRootViewControllerAnimated:YES];

            break;

        case 4:

            //popToView 返回至指定页面

            secondVC = [[self.navigationControllerviewControllers]objectAtIndex:1];

            [self.navigationControllerpopToViewController:secondVCanimated:YES];

            break;

        default:

            break;

    }

}




0 0
原创粉丝点击