我要制作SideMenu。
我的HomeViewController代码:

lazy var sideMenu: SideMenuViewController = {
   let menu = SideMenuViewController()
    menu.stayViewObj = self
    return menu
}()


菜单按钮的操作:

@objc func handleMenuBtn () {
    sideMenu.openMenu()
}


SideMenu的viewDidLoad

override func viewDidLoad(){
        constraintLeadingPropertiesView.constant = -viewSelectProperties.frame.width
}


SideMenu的openMenu方法:

 func openMenu() {
        debugPrint(">>>>openMenu")
        let statusBarHeight = UIApplication.shared.statusBarFrame.height
        let yCoordinate: CGFloat = statusBarHeight + 44.0

        guard let window = UIApplication.shared.keyWindow else { return }
        view.backgroundColor = UIColor(white: 0, alpha: 0.5)
        view.alpha = 0
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(closeMenu)))
        view.frame = window.frame

        startFrame = CGRect(x: 0, y: yCoordinate, width: 0, height: window.frame.height - yCoordinate)
        view.frame = startFrame!

        window.addSubview(view)


        UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            self.view.alpha = 1
            self.view.frame = CGRect(x: 0, y: yCoordinate, width: window.frame.width - 80, height: window.frame.height - yCoordinate)

        }, completion: nil)
    }


现在,我得到constraintLeadingPropertiesView = nil,并且SideMenuController的所有其他属性也都nil接受stayViewObj
所以在viewDidLoad上崩溃。
如何解决这个问题。任何建议对我也有帮助。
谢谢!

最佳答案

需要instantiateInitialViewController而不是通过lazy Var创建对象。

let menu = UIStoryboard.init(name: "Main", bundle: nil).instantiateInitialViewController() as? SideMenuViewController

关于ios - ViewController的属性为nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50713573/

10-11 23:35