天天看點

iOS——Storyboard使用一、segue

一、segue

1. 簡介

使用 storyboard 可以實作在多個 UIViewController 之間跳轉,實作跳轉的對象就是 UIStoryboardSegue 對象

每個 UIStoryboardSegue 都有三個重要的屬性

1)唯一标志

@property (nullable,nonatomic, copy,readonly) NSString *identifier;

2)源 UIViewController

@property (nonatomic,readonly) __kindofUIViewController *sourceViewController;

3)目的 UIViewController 

@property (nonatomic,readonly) __kindofUIViewController *destinationViewController;

2. segue 類型

在 storyboard 中有如下幾個類型 1)push push 類型需要的是一個 UINavigationController 對象,必須是在 UINavigationController 對象中推入下一級的 UIViewController 時使用

2)modal 新出現的場景會完全蓋住舊的那個場景,并且使用者隻能和新的場景互動,無法和舊的場景互動,除非關閉新的場景才可以

3)custom 自定義 segue 的類型

4)popover(僅iPad) 5)replace(僅 iPad)

3. 分類

在 storyboard 中,segue 分為 自動型 和 手動型

1)自動型 

在 storyboard 中拖入兩個 UIViewController 并設定 background 區分,向第一個 UIViewController 中拖入一個 UIButton,用于跳轉第二個 UIViewController,如圖

iOS——Storyboard使用一、segue

點選 UIButton 并按住 control 向第二個 UIViewController 拖動,出現如圖的畫面,并選擇 Modal(模态轉換)

iOS——Storyboard使用一、segue

之後,會在左側的界面出現選中的 segue 類型

iOS——Storyboard使用一、segue

可以設定 segue 的 identifier 使其顯示;一般在自動型的 segue 中是不必設定 identifier 的,需要在 手動型中設定,後面會有介紹

iOS——Storyboard使用一、segue
iOS——Storyboard使用一、segue

此時,點選 UIButton,就會跳到第二個 UIViewController 了

2)手動型

還是和前面的界面一樣,拖入兩個 UIViewController 并設定 background,向第一個 UIViewController 中拖入一個 UIButton

選擇 第一個視圖控制器 并按住 control 向第二個 UIViewController 連線,如圖

iOS——Storyboard使用一、segue
iOS——Storyboard使用一、segue

并選中 Modal 類型

使用 手動型的 segue 必須要設定幾個地方

1)設定 segue 的 identifier,例如 這裡設定 “btnSegue”

2)向 UIButton 添加動作事件

- (IBAction)btnClick:(id)sender {
    
    NSLog(@"%@", NSStringFromSelector(_cmd));
    
    // 通過指定的 identifier 來選擇實作跳轉的 segue 對象,就是我們在 storyboard 中連線的那個 segue
    [self performSegueWithIdentifier:@"btnSegue" sender:nil];
    
}           

3)跳轉之前會調用該方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    NSLog(@"%@", NSStringFromSelector(_cmd));

}           

此時,運作程式點選按鈕,就可以跳轉到第二個 UIViewController ,并且在控制台輸出以下資訊

iOS——Storyboard使用一、segue