摘要: 關(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 controllers is discouraged <>
這樣的警告代碼盗冷,如果你認(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];
[self presentViewController:viewController animated:YES completion:nil];
改成
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
ViewController *viewController = [[ViewController alloc] init];
[delegate.window.rootViewController presentViewController:viewController animated:YES completion:nil];
即可
方法一:就是通過(guò)上面的方法奶段,先找到appdelegate然后獲取window屬性
KLViewController *rootVc = (KLViewController *)[(AppDelegate *)[UIApplication sharedApplication].delegate window].rootViewController;
這種方法除了找根控制器還能找appdelegate類里的定義的任何屬性,如:
- (void) openOrcloseTheList {
AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (tempAppDelegate.theListVC.closed) {
[tempAppDelegate.theListVC openListView];
} else {
[tempAppDelegate.theListVC closeListView];
}
}
方法二:直接通過(guò)keyWindow來(lái)找根控制器,更直接
KLViewController *rootVc = (KLViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
補(bǔ)充:
YZLoginViewController *yzLoginVc = [YZLoginViewController new];
//找根控制器方法
//1.找到appdelegate然后獲取window屬性
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window.rootViewController presentViewController:yzLoginVc animated:YES completion:nil];
//2.直接通過(guò)keyWindow來(lái)找根控制器
YZYuQingViewController *rootVc = (YZYuQingViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
[rootVc presentViewController:yzLoginVc animated:YES completion:nil];