Jessica
Jessica

Reputation: 21

alertView is now alertController

I'm trying to finish off my login page in iOS and it says "value of type 'UIAlertController' has no member 'show'" after I have changed alertView to alertController.

My code is:

if ( username.isEqualToString("") || password.isEqualToString("") ) {

        var alertView:UIAlertController = UIAlertController()
        alertView.title = "Sign Up Failed!"
        alertView.message = "Please enter Username and Password"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else if ( !password.isEqual(confirm_password) ) {

        var alertView:UIAlertController = UIAlertController()
        alertView.title = "Sign Up Failed!"
        alertView.message = "Passwords doesn't Match"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else {

And I get the errors:

'UIAlertController' has no member 'show'" and "Value of type 'UIAlertController' has no member 'delegate'", etc.

Furthermore, since I changed it to alertController this section of code has also got errors:

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:reponseError)

    if ( urlData != nil ) {
        let res = response as! NSHTTPURLResponse!;

        NSLog("Response code: %ld", res.statusCode);

        if (res.statusCode >= 200 && res.statusCode < 300)
        {
            var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

            NSLog("Response ==> %@", responseData);

            var error: NSError?

            let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary

Stating "Extra argument 'error' in call" but if I take the error out, then it just gives me a different error, wanting me to take the ) away and add a , continuously!

Thank you!

Upvotes: 1

Views: 2196

Answers (3)

Piyush Bansal
Piyush Bansal

Reputation: 1723

Try the following code if you create an alert box without any action button.

let alertController = UIAlertController(title: "Alert", message: "This is an alert.", preferredStyle: .alert)


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

Try the following code if you create an alert box with action button.

let alertController = UIAlertController(title: "Alert", message: "This is an alert.", preferredStyle: .alert)

let action1 = UIAlertAction(title: "Default", style: .default) { (action:UIAlertAction) in
    print("You've pressed default");
}

let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
    print("You've pressed cancel");
}

let action3 = UIAlertAction(title: "Destructive", style: .destructive) { (action:UIAlertAction) in
    print("You've pressed the destructive");
}

alertController.addAction(action1)
alertController.addAction(action2)
alertController.addAction(action3)
self.present(alertController, animated: true, completion: nil)

Upvotes: 0

vadian
vadian

Reputation: 285290

The classes UIAlertView are UIAlertController are completely different and are not interchangeable

The equivalent code of UIAlertController is

let alertController = UIAlertController(title: "Sign Up Failed!", 
                                      message: "Please enter Username and Password", 
                               preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)

To resolve the extra argument error you need the new error handling syntax of Swift 2

do {
   let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options:.MutableContainers) as! [String:AnyObject]
} catch {
    print(error)
}

If jsonData is not guaranteed to be a dictionary (the Swift native type is preferable) you need an extra line to check the type with optional binding

do {
   let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options:.MutableContainers)
   if jsonDict = jsonData as? [String:AnyObject] {
      // do something with the dictionary
   }
} catch {
    print(error)
}

PS: A simpler syntax for username.isEqualToString("") is username.isEmpty

Upvotes: 2

Josef B&#252;ttgen
Josef B&#252;ttgen

Reputation: 256

You have to present it like a UIViewController:

self.presentViewController(alertView, animated: true, completion: nil)

Upvotes: 0

Related Questions