3

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 alertView = UIAlertView(
    title: "Hey",
    message: "Hello",
    delegate: self,
    cancelButtonTitle: "Cancel"
)
alertView.show()
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Apple Kid
  • 623
  • 2
  • 7
  • 16

4 Answers4

17

From Xcode 6.0 UIAlertView class:

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

On swift ( 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 'Alert' you need only to change the UIAlertControllerStyle for example:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet)
Guy Kahlon
  • 4,510
  • 4
  • 30
  • 41
  • Can you please tell me how can i add a TextField to it.?? – Apple Kid Jun 07 '14 at 04:02
  • 1
    @AppleKid,add alert.addTextFieldWithConfigurationHandler(configurationTextField), and the function: func configurationTextField(textField: UITextField!) { println("configurat hire the TextField") if let tField = textField { self.textField = textField! self.textField.text = "Hello world" } } Now you can to print the user's input on the handleCancel or handleOk closures: println(self.textField.text) – Guy Kahlon Jun 08 '14 at 19:50
  • Trying to use self.textField.text in the closure but it says `Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit` – User Sep 21 '14 at 17:56
1

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)
0

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()

remember to import on the class UIAlertViewDelegate

-1

Use following way :

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)
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
  • use " func showInView(_ view: UIView!) " to add textField here is ref. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIActionSheet_Class/index.html#//apple_ref/occ/instm/UIActionSheet/showInView: – Divya Bhaloidiya Jun 06 '14 at 14:52
  • i didnt get it.. can you please show me a small code.?? – Apple Kid Jun 06 '14 at 14:56