- 父類是NSObject
-
Storyboard上每一根用來界面跳轉(zhuǎn)的線空镜,都是一個UIStoryboardSegue對象(簡稱Segue)
Snip20160312_2.png
UIStoryboardSegue所有屬性
//唯一標(biāo)識
@property (nonatomic, readonly) NSString *identifier;
//來源控制器
@property (nonatomic, readonly) id sourceViewController;
//目標(biāo)控制器
@property (nonatomic, readonly) id destinationViewController;
UIStoryboardSegue所有方法
- (void)perform;
segue類型:
根據(jù)Segue的執(zhí)行(跳轉(zhuǎn))時刻椭豫,Segue可以分為2大類型
1. 自動型:點(diǎn)擊某個控件后(比如按鈕)浆熔,自動執(zhí)行Segue,自動完成界面跳轉(zhuǎn)
2. 手動型:需要通過寫代碼手動執(zhí)行Segue宏侍,才能完成界面跳轉(zhuǎn)-
自動型
自動型segue.png
triggered segues.png1. 點(diǎn)擊“登錄”按鈕后赖淤,就會自動跳轉(zhuǎn)到右邊的控制器 2. 按住Control鍵,直接從控件拖線到目標(biāo)控制器 3. 如果點(diǎn)擊某個控件后谅河,不需要做任何判斷漫蛔,一定要跳轉(zhuǎn)到下一個界面,建議使用“自動型Segue”
-
手動型
1. 按住Control鍵旧蛾,從來源控制器拖線到目標(biāo)控制器
手動型的Segue.png
2. 手動型的Segue需要設(shè)置一個標(biāo)識(如右圖)
手動型的Segue id.png
triggered segues.png
3. 在恰當(dāng)?shù)臅r刻,使用perform方法執(zhí)行對應(yīng)的Segue
```objc
// Segue必須由來源控制器來執(zhí)行蠕嫁,也就是說锨天,這個perform方法必須由來源控制器來調(diào)用
[sourceViewController performSegueWithIdentifier:@"login2contacts" sender:nil];
```
4. 如果點(diǎn)擊某個控件后,需要做一些判斷剃毒,也就是說:滿足一定條件后才跳轉(zhuǎn)到下一個界面病袄,建議使用“手動型Segue”
performSegueWithIdentifier方法詳解
- 利用performSegueWithIdentifier:方法可以執(zhí)行某個Segue搂赋,完成界面跳轉(zhuǎn)
[sourceViewController performSegueWithIdentifier:@“l(fā)ogin2contacts” sender:nil];
- 完整執(zhí)行過程
// 1.1 生成UIStoryboardSegue對象,并設(shè)置屬性 // 1.2 根據(jù)identifier去storyboard中找到對應(yīng)的線益缠,新建UIStoryboardSegue對象 // 1.3 設(shè)置Segue對象的sourceViewController(來源控制器) // 1.4 新建并且設(shè)置Segue對象的destinationViewController(目標(biāo)控制器) // 生成目標(biāo)控制器 FZQEditController *editVC = [[FZQEditController alloc] init]; /** 創(chuàng)建設(shè)置segue */ UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"contactToEdit" source:self destination:editVC performHandler:^{ // 2.2.1 如果segue的style是push // 取得sourceViewController所在的UINavigationController // 調(diào)用UINavigationController的push方法將destinationViewController壓入棧中脑奠,完成跳轉(zhuǎn) [self.navigationController pushViewController:editVC animated:YES]; // 2.2.2 如果segue的style是modal // 調(diào)用sourceViewController的presentViewController方法將destinationViewController展示出來 // [self presentViewController:editVC animated:YES completion:nil]; }]; // 3 調(diào)用sourceViewController的下面方法,做一些跳轉(zhuǎn)前的準(zhǔn)備工作并且傳入創(chuàng)建好的Segue對象 [self prepareForSegue:segue sender:nil]; // 4. 調(diào)用Segue對象的- (void)perform跳轉(zhuǎn); [segue perform]; // 5. 跳轉(zhuǎn)前傳遞信息 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // 獲取目標(biāo)控制器 FZQEditController *editVC = segue.destinationViewController; // 傳遞數(shù)據(jù) NSIndexPath *indexPath = sender; editVC.index = indexPath.row; editVC.contact = self.contacts[indexPath.row]; } }