您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 開發中 ViewController 的頁面跳轉與彈出模態

編輯:iOS 相關教學

ViewController 頁面跳轉

從一個 Controller 跳轉到另一個 Controller,一般有以下兩種方式:

1、利用 UINavigationController,呼叫 pushViewController 進行跳轉,採用壓棧與出棧的方式管理 Controller;呼叫 popViewControllerAnimated 可以返回。

PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self.navigationController pushViewController:ickImageViewController animated:YES];

2、利用 UIViewController 自身的 presentModalViewController 進行跳轉;呼叫 dismissModalViewControllerAnimated 可以返回。

PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self presentModalViewController:ickImageViewController animated:YES];
// 返回
[self dismissModalViewControllerAnimated:YES];

Present ViewController Modally

一、主要用途

彈出模態 ViewController 是 iOS 開發中很有用的一項技術,UIKit 提供了一些專門用於模態顯示的 ViewController,如 UIImagePickerController 等。它主要用於以下幾種情形:

1、收集使用者輸入資訊。

2、臨時呈現一些內容。

3、臨時改變工作模式。

4、回應設備方向變化(用於針對不同方向分別呈現兩個 ViewController 的情況)。

5、顯示一個新的 view 層級。

這幾種情形都會暫時中斷程式正常的執行流程,主要作用是收集或顯示資訊。

二、幾個概念和常用設定

1、presenting view controller 與 presented view controller

當我們在 view controller A 中模態顯示 view controller B 時,A 就是 presenting view controller(彈出 VC),B 就是 presented view controller(被彈出 VC)。官方建議兩者之間透過 delegate 實現互動。用過 UIImagePickerController 從相簿選照片或拍照的人可以發現,imagePickerController 與彈出它的 VC 之間正是透過 UIImagePickerControllerDelegate 實現互動的。因此實務上最好也遵守這個原則:在被彈出的 VC 中定義 delegate,並在彈出 VC 中實作該代理。

2、Modal Presentation Styles(彈出風格)

透過設定 presenting VC 的 modalPresentationStyle 屬性,可以設定彈出風格:

typedef enum {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet,
    UIModalPresentationFormSheet,
    UIModalPresentationCurrentContext,
} UIModalPresentationStyle;

UIModalPresentationFullScreen 代表彈出時 presented VC 充滿全螢幕,若彈出 VC 的 wantsFullScreenLayout 設為 YES,則會填充到狀態列下邊。

UIModalPresentationPageSheet 代表 presented VC 的高度與當前螢幕高度相同、寬度與豎屏時相同,剩餘未覆蓋區域會變暗並阻止點擊;橫屏時兩邊會留下變暗區域。

UIModalPresentationFormSheet 這種模式下,presented VC 的高與寬均小於螢幕尺寸,居中顯示,四周留下變暗區域。

UIModalPresentationCurrentContext 這種模式下,presented VC 的彈出方式與 presenting VC 的父 VC 相同。

這四種方式在 iPad 上都有效,但在 iPhone 與 iPod touch 上,系統始終以 UIModalPresentationFullScreen 模式顯示 presented VC。

3、Modal Transition Style(彈出時的動畫風格)

透過設定 presented VC 的 modalTransitionStyle 屬性,可以設定轉場動畫風格:

typedef enum {
    UIModalTransitionStyleCoverVertical = 0,
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;

有從底部滑入、水平翻轉進入、交叉溶解以及翻頁這四種風格可選,且不受設備限制,不論 iPhone 還是 iPad 都會依指定風格顯示轉場效果。

4、Dismiss Modal ViewController(關閉彈出的 VC)

關閉 presented VC,可以呼叫以下兩個函式中的任何一個:

dismissModalViewControllerAnimated:      // 將要廢棄,不建議繼續使用
dismissViewControllerAnimated:completion:

誰來呼叫關閉方法?正確的做法是「誰污染誰治理」,即由 presenting VC 呼叫上述方法來關閉 presented VC。這樣有個好處:如果一個 VC 依使用者不同選擇可能彈出不同的 view controller,當不再需要顯示時,直接呼叫 [self dismissModalViewControllerAnimated:] 即可使之消失,而不必關心它具體是哪一類 view controller。系統在此也做了優化:當我們在 presented VC 中呼叫該方法時,系統會自動把訊息傳遞到相應的 presenting VC。具體採用哪一種要看實際情況,若 presented VC 需要與 presenting VC 有資料傳遞,建議在 presenting VC 實作的代理函式中 dismiss 彈出的 view controller。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved