Swift UIActionSheet crashes on iPad?

泄露秘密 提交于 2019-12-01 10:47:57

you need to check the system version in runtime if your project supports both iOS7 and iOS8; you can insert this snippet into any of your methods:

let systemVersion: NSInteger = (UIDevice.currentDevice().systemVersion as NSString).integerValue
if systemVersion < 8 {
    // iOS7:
    let action:UIActionSheet = UIActionSheet(title: "Change Map Type", delegate: self, cancelButtonTitle: "Back", destructiveButtonTitle: nil, otherButtonTitles: "Product Page", "Video")
    action.tag = 0
    action.showInView(self.view)
} else {
    // iOS8:
    let alertController: UIAlertController = UIAlertController(title: "Change Map Type", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
    let cancelAction: UIAlertAction = UIAlertAction(title: "Back", style: UIAlertActionStyle.Cancel, handler: nil)
    let button1action: UIAlertAction = UIAlertAction(title: "Product Page", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
        // doing something for "product page"
    })
    let button2action: UIAlertAction = UIAlertAction(title: "Video", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction!) -> () in
        // doing something for "video"
    })
    alertController.addAction(cancelAction)
    alertController.addAction(button1action)
    alertController.addAction(button2action)

    // for iPAD support:
    alertController.popoverPresentationController?.sourceView = self.view
    alertController.popoverPresentationController?.sourceRect = CGRectMake(self.view.bounds.width / 2.0, self.view.bounds.height / 2.0, 1.0, 1.0) // this is the center of the screen currently but it can be any point in the view

    self.presentViewController(alertController, animated: true, completion: nil)
}

and your class needs to conform the UIActionSheetDelegate for the UIActionSheet class:

extension ViewController : UIActionSheetDelegate {

    func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) {

        if actionSheet.tag == 0 {
            if buttonIndex == 1 {
                // doing something for "product page"
            } else if (buttonIndex == 2) {
                // doing something for "video"
            }
        }
    }

}

also tried this

@IBAction func BTfindme(sender: AnyObject) {
        let actionSheetController: UIAlertController = UIAlertController(title: "Action Sheet", message: "Test", preferredStyle: .ActionSheet)
        let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
            }
        actionSheetController.addAction(cancelAction)

        let ActionOne: UIAlertAction = UIAlertAction(title: "Action one", style: .Default) { action -> Void in
            }
        actionSheetController.addAction(ActionOne)

        let ActionTwo: UIAlertAction = UIAlertAction(title: "Action two", style: .Default) { action -> Void in
            }
        ActionTwo.addAction(choosePictureAction)
        self.presentViewController(actionSheetController, animated: true, completion: nil)
    }

and this also force a crash on ipad but not on iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!