发生崩溃:

navigationController.view.layoutIfNeeded()



  日志:致命错误:解开可选值时意外发现nil


当应用程序首次启动时(在xcode构建之后),以及当我的用户断开连接并从其帐户重新连接时,就会出现崩溃。如果我在已连接用户的情况下重新启动该应用程序,则可以。

全功能代码:

func prepareControllers(){
    homeTabs = [homeView, teamView, rankView, generalView]

    for view in homeTabs{
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ABMainViewController.didTapTab(_:))))
    }

    mainControllers = [
        UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "home") as! ABHomeViewController,
        UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "teamController") as! ABTeamViewController,
        UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "leaderboardController") as! ABLeaderboardViewController,
        ABInformationViewController(nibName: "ABInformationViewController", bundle: Bundle.main)
    ]

    var previousController: UINavigationController? = nil

    for viewController in mainControllers{
        viewController.delegate = self;
        let navigationController = UINavigationController(rootViewController: viewController)
        navigationControllers.append(navigationController)
        navigationController.view.frame = scrollView.bounds
        navigationController.setNavigationBarHidden(true, animated: false)
        navigationController.view.translatesAutoresizingMaskIntoConstraints = false

        scrollView.addSubview(navigationController.view)

        scrollView.addConstraint(NSLayoutConstraint(item: navigationController.view, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1, constant: 0))
        scrollView.addConstraint(NSLayoutConstraint(item: navigationController.view, attribute: .height, relatedBy: .equal, toItem: scrollView, attribute: .height, multiplier: 1, constant: 0))
        scrollView.addConstraint(NSLayoutConstraint(item: navigationController.view, attribute: .width, relatedBy: .equal, toItem: scrollView, attribute: .width, multiplier: 1, constant: 0))

        if(previousController != nil){
            scrollView.addConstraint(NSLayoutConstraint(item: navigationController.view, attribute: .left, relatedBy: .equal, toItem: previousController!.view, attribute: .right, multiplier: 1, constant: 0))
        }else{
            scrollView.addConstraint(NSLayoutConstraint(item: navigationController.view, attribute: .leading, relatedBy: .equal, toItem: scrollView, attribute: .leading, multiplier: 1, constant: 0))
        }

        navigationController.view.layoutIfNeeded()  // CRASH HERE
        previousController = navigationController
    }

    scrollView.addConstraint(NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: previousController!.view, attribute: .trailing, multiplier: 1, constant: 0))
}


在调试导航器中:

    0x102b691f8 <+116>: bl     0x102a5cb80               ; function signature    specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> ()]> of generic specialization <preserving fragile attribute, ()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A
 ->  0x102b691fc <+120>: brk    #0x1    //HERE


有没有办法找到这个解开值在哪里?
如果有人有一个想法,我该如何解决或如何更清楚地对其进行调试。谢谢 !

最佳答案

记录说您在navigationController.view.layoutIfNeeded()中的实例之一为nil。表示navigationControllerview为零。

在调用方法viewDidLoad之前,控制器的所有视图均为零。或者,在您的代码中,实例化UINavigationController并在此之后直接调用它的主视图。

如果您确实要调用layoutIfNeeded方法,请子类UINavigationController并在viewDidLoadviewWillAppearviewDidAppear中调用该方法,即使我认为如果约束得到很好的实现也不必调用它

class CustomNavigationController: UINavigationController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.view.layoutIfNeeded()
    }
}

关于ios - 对layoutIfNeeded()的模糊崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42086941/

10-12 14:16