代碼和storyboard混合使用實(shí)現(xiàn)視圖切換方法

視圖切換一般有三種方法:UINavigationController,UITabbarController,模態(tài)窗口。
一、在一個(gè)storyboard中存在多個(gè)ViewController想實(shí)現(xiàn)他們之間的切換可以使用segue或者代碼實(shí)現(xiàn)切換。使用segue切換就不能在實(shí)現(xiàn)切換過(guò)程處理一些方法。若用代碼如何實(shí)現(xiàn)呢?
1、首先給你需要跳轉(zhuǎn)到的視圖設(shè)置storyboard ID.

1

2、然后根據(jù)Identity和presentViewController方法實(shí)現(xiàn)實(shí)例化一個(gè)視圖并以模態(tài)對(duì)方式顯示。

//這里實(shí)現(xiàn)的是modal切換
GetDataByPropertyViewController *getDataByPropertyViewController = 
(GetDataByPropertyViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"getDataByPropertyViewController"];
getDataByPropertyViewController.data = _userName.text;//通過(guò)屬性傳遞值

[self presentViewController:getDataByPropertyViewController animated:YES completion:nil];

二、在多個(gè)storyboard中存在多個(gè)ViewController。使用多個(gè)storyboard是為了方便管理不同視圖防止在一個(gè)storyboard中存在過(guò)多的視圖而導(dǎo)致混亂。
1、首先也是給storyboard設(shè)置storyboard ID.
2、這時(shí)需要?jiǎng)?chuàng)建并實(shí)例化一個(gè)storyboard。

// 通過(guò)多個(gè)storyboard來(lái)管理不同視圖控制器可以避免放在一個(gè)storyboard中導(dǎo)致凌亂感覺(jué)。
//1.根據(jù)storyboard名字創(chuàng)建storyboard
UIStoryboard *s = [UIStoryboard storyboardWithName:@"second" bundle:nil];
//2.實(shí)例化s中的視圖控制器。
UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"second"];
[self.navigationController pushViewController:vc animated:true];

三、純代碼實(shí)現(xiàn)視圖之間modal切換
直接創(chuàng)建并實(shí)例化視圖后調(diào)用presentViewController方法即可。

GetDataByPropertyViewController *getDataByPropertyViewController = [[GetDataByPropertyViewController alloc]init];

getDataByPropertyViewController.data = _userName.text;//傳遞值
[self presentViewController:getDataByPropertyViewController animated:YES completion:nil];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容