Warning: Attempt to present * on * which is already presenting * Warning: Attempt to present (要被presented的控制器) on (哪个控制器来presenting) which is already presenting (已经被presenting的控制器)

在AppDelegate里面要弹出一个弹框,这个弹框的作用就是:当用户账号在别的客户端登录的时候,弹出提示并引导用户从新登录,但是实际测试中这个弹出效果时好时坏,并爆出了警告

Warning: Attempt to present * on * which is already presenting *Warning: Attempt to present (要被presented的控制器)  on (哪个控制器来presenting) which is already presenting (已经被presenting的控制器).

现在把修改后的弹框代码附上


Warning: Attempt to present * on * which is already presenting * Warning: Attempt to present (要被presented的控制器) on (哪个控制器来presenting) which is already presenting (已经被presenting的控制器)_第1张图片


之前的代码是 先模态推出弹框,在弹框的动作中再模态推出到登录页,这样就容易出现效果时有时无的问题,具体的原因就是:

当你模态推出一个控制器的时候,必须要先dismiss以后,才能推出下个页面,否则就推不出去.

还要就是,在AppDelegate里面弹出弹框时,一定要取出最外层的控制器,用这个控制器推出弹框,否则也容易出现弹框时有时无的问题.



所以,针对以上问题,做出了修改,

首先:弹出弹框依旧使用模态转换来推出,

然后:在弹框里跳转到登录界面采用直接转换根视图的方法,这样就不容易出问题了,代码如图片所示.

(但是这样会有另一个警告: Presenting view controllers on detached view controllers is discouraged. 但是每次弹框还是依然可以正常的弹出,跳转登录页也没什么问题);此处如果有好的解决方案,欢迎私聊



此处分享一个获取当前最外层控制器的方法:


-(UIViewController *)topViewController{

if (!_topViewController) {

_topViewController = [self topViewControllerWithRootViewController:self.view.window.rootViewController];

}

return _topViewController;

}



- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController

{

if ([rootViewController isKindOfClass:[UITabBarController class]]) {

UITabBarController *tabBarController = (UITabBarController *)rootViewController;

return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];

} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {

UINavigationController* navigationController = (UINavigationController*)rootViewController;

return [self topViewControllerWithRootViewController:navigationController.visibleViewController];

} else if (rootViewController.presentedViewController) {

UIViewController* presentedViewController = rootViewController.presentedViewController;

return [self topViewControllerWithRootViewController:presentedViewController];

} else {

return rootViewController;

}

}



里边怎么引用代码啊!!!,我要吐槽,看起来写的乱七八糟.

你可能感兴趣的:(Warning: Attempt to present * on * which is already presenting * Warning: Attempt to present (要被presented的控制器) on (哪个控制器来presenting) which is already presenting (已经被presenting的控制器))