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

來源:互聯網
上載者:User

ViewController 頁面跳轉
從一個Controller跳轉到另一個Controller時,一般有以下2種:
1、利用UINavigationController,調用pushViewController,進行跳轉;這種採用壓棧和出棧的方式,進行Controller的管理。調用popViewControllerAnimated方法可以返回。

複製代碼 代碼如下:

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


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等。彈出模態ViewController主要使用於一下這幾種情形:
 
  1、收集使用者輸入資訊
 
  2、臨時呈現一些內容
 
  3、臨時改變工作模式
 
  4、相應裝置方向變化(用於針對不同方向分別是想兩個ViewController的情況)
 
  5、顯示一個新的view層級
 
  這幾種情形都會暫時中斷程式正常的執行流程,主要作用是收集或者顯示一些資訊。
 
二、幾個概念和常用設定
 
1、presenting view controller Vs 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屬性,我們可以設定彈出View Controller時的風格,有以下四種風格,其定義如下:

複製代碼 代碼如下:

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

  UIModalPresentationFullScreen代表彈出VC時,presented VC充滿全屏,如果彈出VC的wantsFullScreenLayout設定為YES的,則會填充到狀態列下邊,否則不會填充到狀態列之下。
 
  UIModalPresentationPageSheet代表彈出是彈出VC時,presented VC的高度和當前螢幕高度相同,寬度和豎屏模式下螢幕寬度相同,剩餘未覆蓋地區將會變暗並阻止使用者點擊,這種彈出模式下,豎屏時跟UIModalPresentationFullScreen的效果一樣,橫屏時候兩邊則會留下變暗的地區。
 
  UIModalPresentationFormSheet這種模式下,presented VC的高度和寬度均會小於螢幕尺寸,presented VC置中顯示,四周留下變暗地區。
 
  UIModalPresentationCurrentContext這種模式下,presented VC的彈出方式和presenting VC的父VC的方式相同。
 
  這四種方式在iPad上面統統有效,但在iPhone和iPod touch上面系統始終已UIModalPresentationFullScreen模式顯示presented VC。
 
3、Modal Transition Style(彈出時的動畫風格)
 
  通過設定設定presented VC的modalTransitionStyle屬性,我們可以設定彈出presented VC時情境切換動畫的風格,其定義如下:
複製代碼 代碼如下:

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

  我們可以看到有從底部滑入,水平翻轉進入,交叉溶解以及翻頁這四種風格可選。這四種風格在不受裝置的限制,即不管是iPhone還是iPad都會根據我們指定的風格顯示轉場效果。
 
4、Dismiss Modal ViewController(消失彈出的VC)
 
  消失presented VC,我們可以通過調用以下兩個函數中的任何一個來完成
複製代碼 代碼如下:

dismissModalViewControllerAnimated:                 // 將要廢棄,不贊成繼續使用
dismissViewControllerAnimated:completion:

  誰來調用這消失presented VC的這個方法:正確的做法是“誰汙染誰治理”,即presenting VC調用上面的方法來取消presented VC的顯示。這樣做有一個好處,如果一個VC真不使用者做的不同選擇可能彈出不同的view controller,當不再需要顯示被彈出的view controller的時候,直接調用[self dismissModalViewControllerAnimated]即可使之消失,而不用去關心其具體顯示的哪一類view controller。當然系統在這裡做了最佳化,當我們在presented VC裡面調用上面的方法的時候,系統會自動的將這個訊息傳遞到相應的presenting VC中,這樣就可以實現不管誰彈出了自己,當不再需要的時候直接將自己消失掉的功能。在應用中具體要採用那種要看具體情況,如果presented VC需要和presenting VC有資料傳遞的話,建議在presenting VC實現的代理函數中dismiss彈出的view controller。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.