UIAlertView is Not Working in Swift

后端 未结 4 1724
执笔经年
执笔经年 2020-12-10 00:09

when i runt this code in swift, i dont know why the app terminates by showing a break point in the \"alertView.show()\" part, Somebody please help me.

var al         


        
4条回答
  •  粉色の甜心
    2020-12-10 00:28

    UIAlertView is deprecated in iOS 8, But Swift supports iOS7 and you can not use UIAlertController on iOS 7. Add the following method to solve the issue :

    func showAlert(title:NSString, message:NSString,owner:UIViewController) {
        if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
            var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            owner.presentViewController(alert, animated: true, completion: nil)
        }
        else {
            let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
            alertView.alertViewStyle = .Default
            alertView.show()
        }
    }
    

    and call the method anywhere from the code like this :

    showAlert(APP_NAME,message: "Add your alert message here" ,owner: self)
    

提交回复
热议问题