Terminating app due to uncaught

2019-08-22  本文已影响0人  蛋哥是只猫

在项目开发中使用 popVC?.modalPresentationStyle = UIModalPresentationStyle.popover 做弹窗的时候出现了崩溃,报错

Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController 
(<UIPopoverPresentationController: 0x1042a5cc0>) should have a non-nil sourceView or barButtonItem set before the 
presentation occurs.'

我的代码

        let  popVC = TestPopViewController()
        popVC.popoverPresentationController?.barButtonItem = UIBarButtonItem(customView: customView)
        popVC.popoverPresentationController?.backgroundColor = UIColor(hexNumber: 0x000000, alpha: 0.5)
        popVC?.modalPresentationStyle = UIModalPresentationStyle.popover
        popVC?.popoverPresentationController?.delegate = self
        present(popVC, animated: true, completion: nil)

最后发现在使用Controller 的属性modalPresentationStyle 设置其样式以及给Controller设置其他属性的时候一定要先设置好modalPresentationStyle,然后再设置其他属性
正确的使用方法

        let  popVC = TestPopViewController()
        popVC?.modalPresentationStyle = UIModalPresentationStyle.popover
        popVC.popoverPresentationController?.barButtonItem = UIBarButtonItem(customView: customView)
        popVC.popoverPresentationController?.backgroundColor = UIColor(hexNumber: 0x000000, alpha: 0.5)
        popVC?.popoverPresentationController?.delegate = self
        present(popVC, animated: true, completion: nil)
上一篇 下一篇

猜你喜欢

热点阅读