萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 手機 >  >> 手機應用 >> iPhone

iOS 開發:Storyboard Segue 類型詳解與視圖控制器跳轉傳值實戰

Segue 類型差異

iPhone 支援三種

  • Push:需嵌入 Navigation Controller,新頁面從右側滑入,保留導航欄返回鍵;
  • Modal:新頁面完全覆蓋舊頁(模態),用戶必須關閉才能返回,支援多種轉場動畫;
  • Custom:完全自定義轉場動畫與呈現邏輯。

iPad 額外支援兩種

  • Popover:浮動彈出視圖(如 iPad 版設定選單);
  • Replace:替換當前容器內容(常用於 Split View Controller 次要視圖)。

視圖間傳值核心模式

觸發 Segue 時系統自動調用 prepareForSegue:sender:,在此方法中向目標控制器注入數據:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"Segue ID: %@", segue.identifier);
    UIViewController *dest = segue.destinationViewController;
    if ([dest respondsToSelector:@selector(setData:)]) {
        [dest setValue:@"要傳遞的數據" forKey:@"data"];
    }
}

程式碼跳轉三種場景

  1. Storyboard 已有 Segue:performSegueWithIdentifier:sender:;
  2. 目標 VC 在 Storyboard 但無 Segue:[storyboard instantiateViewControllerWithIdentifier:] 後手動 present/push;
  3. 目標 VC 純代碼創建:直接 alloc/init 後跳轉。

掌握 Segue 類型與傳值模式,是 iOS 界面流程開發的基石。

copyright © 萬盛學電腦網 all rights reserved