- 父类是NSObject
-
Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue)
UIStoryboardSegue所有属性
//唯一标识
@property (nonatomic, readonly) NSString *identifier;
//来源控制器
@property (nonatomic, readonly) id sourceViewController;
//目标控制器
@property (nonatomic, readonly) id destinationViewController;
UIStoryboardSegue所有方法
- (void)perform;
segue类型:
根据Segue的执行(跳转)时刻,Segue可以分为2大类型
1. 自动型:点击某个控件后(比如按钮),自动执行Segue,自动完成界面跳转
2. 手动型:需要通过写代码手动执行Segue,才能完成界面跳转-
自动型
1. 点击“登录”按钮后,就会自动跳转到右边的控制器 2. 按住Control键,直接从控件拖线到目标控制器 3. 如果点击某个控件后,不需要做任何判断,一定要跳转到下一个界面,建议使用“自动型Segue”
-
手动型
1. 按住Control键,从来源控制器拖线到目标控制器
2. 手动型的Segue需要设置一个标识(如右图)
3. 在恰当的时刻,使用perform方法执行对应的Segue
```objc
// Segue必须由来源控制器来执行,也就是说,这个perform方法必须由来源控制器来调用
[sourceViewController performSegueWithIdentifier:@"login2contacts" sender:nil];
```
4. 如果点击某个控件后,需要做一些判断,也就是说:满足一定条件后才跳转到下一个界面,建议使用“手动型Segue”
performSegueWithIdentifier方法详解
- 利用performSegueWithIdentifier:方法可以执行某个Segue,完成界面跳转
[sourceViewController performSegueWithIdentifier:@“login2contacts” sender:nil];
- 完整执行过程
// 1.1 生成UIStoryboardSegue对象,并设置属性 // 1.2 根据identifier去storyboard中找到对应的线,新建UIStoryboardSegue对象 // 1.3 设置Segue对象的sourceViewController(来源控制器) // 1.4 新建并且设置Segue对象的destinationViewController(目标控制器) // 生成目标控制器 FZQEditController *editVC = [[FZQEditController alloc] init]; /** 创建设置segue */ UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"contactToEdit" source:self destination:editVC performHandler:^{ // 2.2.1 如果segue的style是push // 取得sourceViewController所在的UINavigationController // 调用UINavigationController的push方法将destinationViewController压入栈中,完成跳转 [self.navigationController pushViewController:editVC animated:YES]; // 2.2.2 如果segue的style是modal // 调用sourceViewController的presentViewController方法将destinationViewController展示出来 // [self presentViewController:editVC animated:YES completion:nil]; }]; // 3 调用sourceViewController的下面方法,做一些跳转前的准备工作并且传入创建好的Segue对象 [self prepareForSegue:segue sender:nil]; // 4. 调用Segue对象的- (void)perform跳转; [segue perform]; // 5. 跳转前传递信息 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // 获取目标控制器 FZQEditController *editVC = segue.destinationViewController; // 传递数据 NSIndexPath *indexPath = sender; editVC.index = indexPath.row; editVC.contact = self.contacts[indexPath.row]; } }