美文网首页
Terminating app due to uncaught

Terminating app due to uncaught

作者: 蛋哥是只猫 | 来源:发表于2019-08-22 10:43 被阅读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)

相关文章

网友评论

      本文标题:Terminating app due to uncaught

      本文链接:https://www.haomeiwen.com/subject/upggsctx.html