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的控制器).

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




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

当你模态推出一个控制器的时候,必须要先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;

}

}



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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容