天天看點

IOS開發之——使用Segue在StoryBoard之間切換

使用Segue可以在ViewController之間來回切換,下面就來說下切換方法:

1. 使用點選按鈕進行切換

直接上圖,在需要切換的View屬性界面,點選Modal然後拉到前一個view界面或者是Button上

IOS開發之——使用Segue在StoryBoard之間切換

2. 手動進行跳轉

如果拉到了Button的TouchUpInside上,那麼點選左側按鈕的時候就會切到右邊的View,如果拉到了view上,就會連接配接Manual,在代碼中實作跳轉

IOS開發之——使用Segue在StoryBoard之間切換

設定Segue的Indentifier屬性:

IOS開發之——使用Segue在StoryBoard之間切換

代碼中手動進行跳轉:

//在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函數,拉到想要執行的按鈕上,就可以實作跳轉了

IOS開發之——使用Segue在StoryBoard之間切換

也可代碼實作傳回上一個頁面,登出目前頁面:

-(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];
}