關(guān)于Presenting view controllers on detached view controllers is discouraged警告解決

摘要: 關(guān)于形如 Presenting view controllers on detached view controllers is discouraged 的處理

使用模態(tài)跳轉(zhuǎn)時(shí),Xcode有時(shí)候會(huì)出現(xiàn)如下警告

Presenting view controllers on detached view controllersisdiscouraged <>

這樣的警告代碼,如果你認(rèn)為你的層次之間沒(méi)有問(wèn)題(其實(shí)就是層次問(wèn)題。present出來(lái)的模態(tài)窗口,禁止再使用present 來(lái)彈出其它的子窗口)只要把self直接模態(tài)跳轉(zhuǎn)頁(yè)面改成從根控制器跳轉(zhuǎn)即可

解決方法:

ViewController *viewController = [[ViewController alloc] init];[selfpresentViewController:viewController animated:YEScompletion:nil];

改成

AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;ViewController *viewController = [[ViewController alloc] init];[delegate.window.rootViewController presentViewController:viewController animated:YEScompletion:nil];

即可

方法一:就是通過(guò)上面的方法,先找到appdelegate然后獲取window屬性

KLViewController *rootVc = (KLViewController *)[(AppDelegate *)[UIApplication sharedApplication].delegatewindow].rootViewController;

這種方法除了找根控制器還能找appdelegate類里的定義的任何屬性,如:

- (void) openOrcloseTheList {? ? ? ? AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplicationsharedApplication] delegate];if(tempAppDelegate.theListVC.closed) {? ? ? ? [tempAppDelegate.theListVC openListView];? ? }else{? ? ? ? [tempAppDelegate.theListVC closeListView];? ? }}

方法二:直接通過(guò)keyWindow來(lái)找根控制器,更直接

KLViewController *rootVc = (KLViewController *)[UIApplicationsharedApplication].keyWindow.rootViewController;

補(bǔ)充:

YZLoginViewController *yzLoginVc = [YZLoginViewController new];//找根控制器方法//1.找到appdelegate然后獲取window屬性AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;[delegate.window.rootViewController presentViewController:yzLoginVc animated:YEScompletion:nil];//2.直接通過(guò)keyWindow來(lái)找根控制器YZYuQingViewController *rootVc = (YZYuQingViewController *)[UIApplicationsharedApplication].keyWindow.rootViewController;[rootVc presentViewController:yzLoginVc animated:YEScompletion:nil];

鏈接:http://www.itdecent.cn/p/157cab691ad2

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容