Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

I have see more so answers , but nothing helped.Here is my older alert and action for that

override func viewWillAppear(animated: Bool) {
    if Reachability.isConnectedToNetwork() == true {
        print("internet connection ok")
    } else 
    {
        print("internet not ok")
        let alertView: UIAlertView = UIAlertView(title: "Alert ", message: "connect to internet", delegate: self, cancelButtonTitle: "settings", otherButtonTitles: "cancel")
        alertView.show()
        return       
    }       
}

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
{
    if buttonIndex == 0 {
        //This will open ios devices wifi settings
        UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!)
    }
    else if buttonIndex == 1
    {
        //TODO for cancel
        exit(0) 
    }
}

In that i am getting warning :

'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

I tried :

let alert = UIAlertController(title: "Alert", message: "My Alert for test", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: {    (action:UIAlertAction!) in 
        print("you have pressed the Cancel button")
    }))
self.presentViewController(alert, animated: true, completion: nil)

But to add two button and add the index path of button press method link my older code,I am not able to do that. Nothing action happening fro my uialert button,

Please help me out,How can i remove that warnings and recode my Uialert with my two button action.

I am new to swift.Your help will be useful.Thanks!

like image 608
user5513630 Avatar asked Apr 04 '16 05:04

user5513630


5 Answers

See this Code Destructive and OK buttons in UIAlertController:

let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) {
    (result : UIAlertAction) -> Void in
    print("Destructive")
}

// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
    (result : UIAlertAction) -> Void in
    print("OK")
}

alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

Swift 3:

let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert

let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.destructive) {
                        (result : UIAlertAction) -> Void in
    print("Destructive")
}

                    // Replace UIAlertActionStyle.Default by UIAlertActionStyle.default

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                        (result : UIAlertAction) -> Void in
    print("OK")
}

alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)

See Alert With Destructive and OK Button:

enter image description here

like image 57
Kirit Modi Avatar answered Nov 19 '22 06:11

Kirit Modi


In Swift 3, you can write this:

let alertController = UIAlertController(title: "Title", message: "This is my text", preferredStyle: UIAlertControllerStyle.alert)

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)  
{ 
     (result : UIAlertAction) -> Void in
      print("You pressed OK")
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
like image 41
Fred Sousa Avatar answered Nov 19 '22 06:11

Fred Sousa


For a basic alert message I like using an extension on UIViewController:

extension UIViewController {
func alertMessageOk(title: String, message: String) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
    alert.addAction(action)
    present(alert, animated: true, completion: nil)
   }
}

Usage: self.alertMessageOk(title: "Test Title", message: "Test message")

like image 39
9BallOnTheSnap Avatar answered Nov 19 '22 06:11

9BallOnTheSnap


Swift 3

    // Create message
     let alertController = UIAlertController(title: "Title",
                                           message: "Message",
                                    preferredStyle: .actionSheet)

    // Clear Action
    let clearAction = UIAlertAction(title: "Clear",
                                    style: .destructive,
                                  handler: { (action:UIAlertAction!) in
                         print ("clear")
    })
    alertController.addAction(clearAction)

    // Cancel
    let cancelAction = UIAlertAction(title: "Cancel",
                                     style: .cancel,
                                   handler: { (action:UIAlertAction!) in
                                print ("Cancel")
    })
    alertController.addAction(cancelAction)

    // Present Alert
    self.present(alertController,
                 animated: true,
                 completion:nil)
like image 42
Sébastien REMY Avatar answered Nov 19 '22 07:11

Sébastien REMY


  UIAlertController *AC = UIAlertController.alertControllerWithTitle("Title",message:"Message",preferredStyle:UIAlertControllerStyleAlert)

  UIAlertAction *ActionOne = [UIAlertAction actionWithTitle:"ActionOne" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionOne")
} ]

  UIAlertAction *ActionTwo = [UIAlertAction actionWithTitle:"ActionTwo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog("ActionTwo")
} ]
AC.addAction(ActionOne)
AC.addAction(ActionTwo)
self.presentViewController(AC,animated:true,completion:nil)
like image 36
Himanshu jamnani Avatar answered Nov 19 '22 07:11

Himanshu jamnani