-
Storyboard上每一根用來界面跳轉的線,都是一個UIStoryboardSegue對象(簡稱Segue)
1 - Segue的屬性
每一個Segue對象,都有3個屬性
// 唯一標識
@property (nonatomic, readonly) NSString *identifier;
// 來源控制器
@property (nonatomic, readonly) id sourceViewController;
// 目標控制器
@property (nonatomic, readonly) id destinationViewController;

2 - Segue的類型
根據(jù)Segue的執(zhí)行(跳轉)時刻,Segue可以分為2大類型
- 自動型:點擊某個控件后(比如按鈕),自動執(zhí)行Segue,自動完成界面跳轉
- 手動型:需要通過寫代碼手動執(zhí)行Segue,才能完成界面跳轉
2.1 - 自動型Segue
-
按住Control鍵,直接從控件拖線到目標控制器
- 點擊“登錄”按鈕后,就會自動跳轉到右邊的控制器
- 如果點擊某個控件后,不需要做任何判斷,一定要跳轉到下一個界面
建議使用“自動型Segue”
2.2 - 手動型Segue
-
按住Control鍵,從來源控制器拖線到目標控制器
-
手動型的Segue需要設置一個標識
Snip20150831_23.png 在恰當?shù)臅r刻,使用perform方法執(zhí)行對應的Segue
// Segue必須由來源控制器來執(zhí)行,也就是說,這個perform方法必須由來源控制器來調用
[self performSegueWithIdentifier:@"login2contacts" sender:nil];`
- 如果點擊某個控件后,需要做一些判斷
也就是說:滿足一定條件后才跳轉到下一個界面,建議使用“手動型Segue”
3 - performSegueWithIdentifier:sender:
利用performSegueWithIdentifier:方法可以執(zhí)行某個Segue,完成界面跳轉
3.1 - performSegueWithIdentifier:sender:方法`的完整執(zhí)行過程
// self是來源控制器
[self performSegueWithIdentifier:@“l(fā)ogin2contacts” sender:nil];
3.1.1 - 根據(jù)identifier去storyboard中找到對應的線,新建UIStoryboardSegue對象
- 設置Segue對象的sourceViewController(來源控制器)
-
新建并且設置Segue對象的destinationViewController(目標控制器)
Snip20150831_26.png
3.1.2 - 調用sourceViewController的下面方法,做一些跳轉前的準備工作并且傳入創(chuàng)建好的Segue對象
// sender是當初performSegueWithIdentifier:sender:中傳入的sender
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
3.1.3 - 調用Segue對象的- (void)perform;方法開始執(zhí)行界面跳轉操作
3.1.3.1 - 如果segue的style是push
- 取得
sourceViewController所在的UINavigationController - 調用
UINavigationController的push方法將destinationViewController壓入棧中,完成跳轉
3.1.3.2 - 如果segue的style是modal
調用sourceViewController的presentViewController方法將destinationViewController展示出來
######4 - Sender參數(shù)的傳遞





