使用Segue可以在ViewController之間來回切換,下面就來說下切換方法:
1. 使用點擊按鈕進行切換
直接,在需要切換的View屬性介面,點擊Modal然後拉到前一個view介面或者是Button上
2. 手動進行跳轉
如果拉到了Button的TouchUpInside上,那麼點擊左側按鈕的時候就會切到右邊的View,如果拉到了view上,就會串連Manual,在代碼中實現跳轉
設定Segue的Indentifier屬性:
代碼中手動進行跳轉:
//在viewDidAppear裡面添加觸發segue進行自動跳轉-(void)viewDidAppear:(BOOL)animated{ [self performSegueWithIdentifier:@"drawecg" sender:self];}
註:在ViewDidLoad實現跳轉無效
3. 如何跳轉到任意一個頁面
在有可能進行上級跳轉的ViewController檔案中加上下面代碼,函數名稱任起:
#pragma mark 定義這個函數,別的ViewController在Exit的時候就能直接跳到這了- (IBAction)goHome:(UIStoryboardSegue *)segue{ [[segue sourceViewController] class];}
在想要跳轉view的Exit上右鍵,選擇這個goHome函數,拉到想要執行的按鈕上,就可以實現跳轉了
也可代碼實現返回上一個頁面,登出當前頁面:
-(void)lastPage{ NSLog(@"點擊了上一個視圖按鈕"); [self dismissViewControllerAnimated:YES completion:^(void){ // Code }];}
也可這樣實現:
// 擷取故事板 UIStoryboard *board = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; // 擷取故事板中某個View UIViewController *next = [board instantiateViewControllerWithIdentifier:@"Second"]; // 跳轉 [self presentModalViewController:next animated:YES];
當然,如果你使用Navigation Controller,使用Push進行串連,就不是上面的代碼進行跳轉了:
跳轉到LandscapeViewController
//開啟一個橫屏介面- (IBAction)openLandscapeControl:(id)sender { LandscapeViewController *control = [[LandscapeViewController alloc]initWithNibName:@"LandscapeViewController" bundle:nil]; [self.navigationController pushViewController:control animated:YES];}使用pop返回上一個View
//返回前一頁- (IBAction)clickBack:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES];}