摘要: 關(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