whose view is not in the window hierarchy!

今天遇到一个问题简单描述一下:


需求:登录成功之后,需要判断是否绑定手机,没有绑定present一个手机页面,然后Tabbar切换到首页。这个需求还可以,但是业务里登录,注册,忘记密码等等都是modal出来的。所以登陆完之后不得不全部dissmiss掉,然后再通知到我的界面做相应操作。


遇到问题:

 whose view is not in the window hierarchy!

分析:也就是你现在的Present的视图并不是Windows视图,因为现在还有没有dismiss掉的视图。


解决:1.如果是用错顶层视图的可以找到顶层视图。  2,如果没有dismiss掉就进行presnt的可以,dissmiss  callback进行处理。


代码(Swift):

扫描二维码关注公众号,回复: 1477874 查看本文章


找到顶层视图:

///获取当前控制器
func currentVc() ->UIViewController{
    
    var vc = UIApplication.shared.keyWindow?.rootViewController
    
    if (vc?.isKind(of: UITabBarController.self))! {
        vc = (vc as! UITabBarController).selectedViewController
    }else if (vc?.isKind(of: UINavigationController.self))!{
        vc = (vc as! UINavigationController).visibleViewController
    }else if ((vc?.presentedViewController) != nil){
        vc =  vc?.presentedViewController
    }
        
    return vc!
    
}

dismiss所有的界面:

///所有页面都diss到根目录
func dissAllToRoot(currentVc:UIViewController,completion: (() -> Swift.Void)? = nil){
    var vc = currentVc.presentingViewController
    if vc?.presentingViewController == nil {
        currentVc.dismiss(animated: true, completion: completion)
    }
    
    while ((vc?.presentingViewController) != nil) {
        vc = vc?.presentingViewController
    }
    
    vc?.dismiss(animated: true, completion: completion)
}


最后就简单了

 dissAllToRoot(currentVc: self) {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "****"), object: nil)
        }




猜你喜欢

转载自blog.csdn.net/zy_flyway/article/details/79649705