天天看点

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