iPad点击按钮弹出alert对话框崩溃

来源:互联网 发布:阿瓦隆b族洗发水 知乎 编辑:程序博客网 时间:2024/05/17 09:31

错误信息

Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller’s popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.’

解决办法

http://stackoverflow.com/questions/26039229/swift-uialtertcontroller-actionsheet-ipad-ios8-crashes
修改前代码

@IBAction func alert(sender: UIButton) {        let alertController = UIAlertController(title: "My first app", message: "Hello World", preferredStyle: .ActionSheet)        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))        self.presentViewController(alertController, animated: true, completion: nil)    }

修改后代码

@IBAction func alert(sender: UIButton) {        let alertController = UIAlertController(title: "My first app", message: "Hello World", preferredStyle: .ActionSheet)        //ipad使用,不加ipad上会崩溃        if let popoverController = alertController.popoverPresentationController {            popoverController.sourceView = sender            popoverController.sourceRect = sender.bounds        }        alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))        self.presentViewController(alertController, animated: true, completion: nil)    }
0 0
原创粉丝点击