ViewController 頁面跳轉(zhuǎn)
從一個Controller跳轉(zhuǎn)到另一個Controller時勺美,一般有以下2種:
1伍派、利用UINavigationController切厘,調(diào)用pushViewController拦宣,進行跳轉(zhuǎn)溢十;這種采用壓棧和出棧的方式垮刹,進行Controller的管理。調(diào)用popViewControllerAnimated方法可以返回张弛。
復(fù)制代碼 代碼如下:
PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self.navigationController pushViewController: ickImageViewController animated:true];
[ickImageViewController release];
2荒典、利用UIViewController自身的presentModalViewController宗挥,進行跳轉(zhuǎn);調(diào)用dismissModalViewControllerAnimated方法可以返回种蝶。
復(fù)制代碼 代碼如下:
PickImageViewController *ickImageViewController = [[PickImageViewController alloc] init];
[self presentModalViewController:ickImageViewController animated:YES];
//返回
[self dismissModalViewControllerAnimated:YES];
Present ViewController Modally
一、主要用途
彈出模態(tài)ViewController是IOS變成中很有用的一個技術(shù)瞒大,UIKit提供的一些專門用于模態(tài)顯示的ViewController螃征,如UIImagePickerController等。彈出模態(tài)ViewController主要使用于一下這幾種情形:
1透敌、收集用戶輸入信息
2盯滚、臨時呈現(xiàn)一些內(nèi)容
3、臨時改變工作模式
4酗电、相應(yīng)設(shè)備方向變化(用于針對不同方向分別是想兩個ViewController的情況)
5魄藕、顯示一個新的view層級
這幾種情形都會暫時中斷程序正常的執(zhí)行流程,主要作用是收集或者顯示一些信息撵术。
二背率、幾個概念和常用設(shè)置
1、presenting view controller Vs presented view controller
當(dāng)我們在view controller A中模態(tài)顯示view controller B的時候嫩与,A就充當(dāng)presenting view controller(彈出VC)寝姿,而B就是presented view controller(被彈出VC)。官方文檔建議這兩者之間通過delegate實現(xiàn)交互划滋,如果使用過UIImagePickerController從系統(tǒng)相冊選取照片或者拍照饵筑,我們可以發(fā)現(xiàn)imagePickerController和彈出它的VC之間就是通過UIImagePickerControllerDelegate實現(xiàn)交互的。因此我們在實際應(yīng)用用处坪,最好也遵守這個原則根资,在被彈出的VC中定義delegate,然后在彈出VC中實現(xiàn)該代理同窘,這樣就可以比較方便的實現(xiàn)兩者之間的交互玄帕。
2、Modal Presentation Styles(彈出風(fēng)格)
通過設(shè)置presenting VC的modalPresentationStyle屬性塞椎,我們可以設(shè)置彈出View Controller時的風(fēng)格桨仿,有以下四種風(fēng)格,其定義如下:
復(fù)制代碼 代碼如下:
typedef enum {
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
} UIModalPresentationStyle;
UIModalPresentationFullScreen代表彈出VC時案狠,presented VC充滿全屏服傍,如果彈出VC的wantsFullScreenLayout設(shè)置為YES的,則會填充到狀態(tài)欄下邊骂铁,否則不會填充到狀態(tài)欄之下吹零。
UIModalPresentationPageSheet代表彈出是彈出VC時,presented VC的高度和當(dāng)前屏幕高度相同拉庵,寬度和豎屏模式下屏幕寬度相同灿椅,剩余未覆蓋區(qū)域?qū)儼挡⒆柚褂脩酎c擊,這種彈出模式下,豎屏?xí)r跟UIModalPresentationFullScreen的效果一樣茫蛹,橫屏?xí)r候兩邊則會留下變暗的區(qū)域操刀。
UIModalPresentationFormSheet這種模式下,presented VC的高度和寬度均會小于屏幕尺寸婴洼,presented VC居中顯示骨坑,四周留下變暗區(qū)域。
UIModalPresentationCurrentContext這種模式下柬采,presented VC的彈出方式和presenting VC的父VC的方式相同欢唾。
這四種方式在iPad上面統(tǒng)統(tǒng)有效,但在iPhone和iPod touch上面系統(tǒng)始終已UIModalPresentationFullScreen模式顯示presented VC粉捻。
3礁遣、Modal Transition Style(彈出時的動畫風(fēng)格)
通過設(shè)置設(shè)置presented VC的modalTransitionStyle屬性,我們可以設(shè)置彈出presented VC時場景切換動畫的風(fēng)格肩刃,其定義如下:
復(fù)制代碼 代碼如下:
typedef enum {
UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl,
} UIModalTransitionStyle;
我們可以看到有從底部滑入祟霍,水平翻轉(zhuǎn)進入,交叉溶解以及翻頁這四種風(fēng)格可選盈包。這四種風(fēng)格在不受設(shè)備的限制浅碾,即不管是iPhone還是iPad都會根據(jù)我們指定的風(fēng)格顯示轉(zhuǎn)場效果。
4续语、Dismiss Modal ViewController(消失彈出的VC)
消失presented VC垂谢,我們可以通過調(diào)用以下兩個函數(shù)中的任何一個來完成
復(fù)制代碼 代碼如下:
dismissModalViewControllerAnimated:? ? ? ? ? ? ? ? // 將要廢棄,不贊成繼續(xù)使用
dismissViewControllerAnimated:completion:
誰來調(diào)用這消失presented VC的這個方法:正確的做法是“誰污染誰治理”疮茄,即presenting VC調(diào)用上面的方法來取消presented VC的顯示滥朱。這樣做有一個好處,如果一個VC真不用戶做的不同選擇可能彈出不同的view controller力试,當(dāng)不再需要顯示被彈出的view controller的時候徙邻,直接調(diào)用[self dismissModalViewControllerAnimated]即可使之消失,而不用去關(guān)心其具體顯示的哪一類view controller畸裳。當(dāng)然系統(tǒng)在這里做了優(yōu)化缰犁,當(dāng)我們在presented VC里面調(diào)用上面的方法的時候,系統(tǒng)會自動的將這個消息傳遞到相應(yīng)的presenting VC中怖糊,這樣就可以實現(xiàn)不管誰彈出了自己帅容,當(dāng)不再需要的時候直接將自己消失掉的功能。在應(yīng)用中具體要采用那種要看具體情況伍伤,如果presented VC需要和presenting VC有數(shù)據(jù)傳遞的話并徘,建議在presenting VC實現(xiàn)的代理函數(shù)中dismiss彈出的view controller。