UIAlertView not working in Swift

when I run this code in swift, I don't know why the application crashes by showing a breakpoint in the alertView.show () part. Someone please help me.

var alertView = UIAlertView( title: "Hey", message: "Hello", delegate: self, cancelButtonTitle: "Cancel" ) alertView.show() 
+3
ios swift uialertview
Jun 06 '14 at 14:40
source share
4 answers

From Xcode 6.0 UIAlertView Class:

UIAlertView is deprecated. Use a UIAlertController with a preferredStyle instead of a UIAlertControllerStyleAlert.

In fast (iOS 8 and OS X 10.10) you can do this:

 var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel)) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in println("User click Ok button") })) self.presentViewController(alert, animated: true, completion: nil) func handleCancel(alertView: UIAlertAction!) { println("User click cancel button") } 

If you want to use in "ActionSheet" instead of "Alert", you only need to change the UIAlertControllerStyle, for example:

 var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet) 
+17
Jun 06 '14 at
source share

UIAlertView is deprecated in iOS 8, but Swift supports iOS7, and you cannot use UIAlertController on iOS 7. Add the following method to solve the problem:

 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 somewhere from the code as follows:

 showAlert(APP_NAME,message: "Add your alert message here" ,owner: self) 
+1
Jan 19 '15 at 12:32
source share

the best way for me is ...

 class ViewController: UIViewController, UIAlertViewDelegate { var allarme = UIAlertView(title: "Warning", message: "This is a best way to create a alarm message", delegate: self, cancelButtonTitle: "OK") allarme.show() 

don't forget to import into the UIAlertViewDelegate class

0
Feb 01 '15 at 7:14
source share

Use the following method:

 var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert) altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(altMessage, animated: true, completion: nil) 
-one
Jun 06 '14 at
source share



All Articles