星期五, 5月 30, 2014

[iOS] 使用Navigation Controller 切換ViewController

今天練習透過用Storyboard viewcontroller將畫面跳轉至xib viewcontroller。

主畫面storyboard的MultiViewViewController.h
//呼叫xib視窗
-(IBAction)twoXibClicked:(id)sender{

    //產生一個xib的ctrl instance
    
    TwoViewController *twoViewCtrl = [[TwoViewController alloc]
                                                  initWithNibName:@"TwoViewController"
                                                  bundle:nil];

    //透過navigationcontroller推到xib的controller
    [self.navigationController pushViewController:twoViewCtrl animated:YES];
    
    
//    [twoViewCtrl release];
}

使用xib的TwoViewController.m
- (IBAction)xibBackToMainClicked:(id)sender {
    
    //使用navigation退回主畫面
    [self.navigationController popToRootViewControllerAnimated:YES];
}

找到這篇討論Navigation多次跳轉的問題: navigationController pushViewController 多次跳转后怎么返回


返回根页面vc用:
[self.navigationController popToRootViewController]
返回指定的某个vc用下面(通过index定位)
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
或(通过class定位):
for (UIViewController *controller in self.navigationController.viewControllers) {

    if ([controller isKindOfClass:[你要跳转到的Controller class]]) {

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

    }

}

沒有留言:

張貼留言

留個話吧:)