ViewController的属性为零

时间:2018-06-06 06:29:57

标签: ios swift

我想制作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上崩溃 如何解决这个问题...任何建议对我都有帮助..
谢谢!

1 个答案:

答案 0 :(得分:0)

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

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