Swift 2.0 doesn't let me make UIAlertAction

1.4k Views Asked by At

this picture should say enough: enter image description here

I have been scouring the web to find answers and none else seems to have this problem. I am using Xcode 7 Beta 3 with Swift 2. I can't figure out what's wrong.

Thanks in advance

EDIT:

Here's the code:

func input(text:String) -> String {
    Say(text)
    let alert:UIAlertController = UIAlertController(title: "Goog", message: text, preferredStyle: UIAlertControllerStyle.Alert)
    [alert .addTextFieldWithConfigurationHandler({ (textfield: UITextField!) -> Void in
    })]
    var returnValue:String = ""
    let confirmAction = UIAlertAction(
        title: "OK", style: UIAlertActionStyle.Default) {(action) in
            returnValue = (alert.textFields[0] as UITextField).text!
            return
    }
    return returnValue
}
2

There are 2 best solutions below

1
On
UIAlertController *altSuccess = [UIAlertController alertControllerWithTitle:@"Success" message:@"Login Successfully" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *okSuccess = [UIAlertAction
                                    actionWithTitle:@"OK"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                        NSLog(@"%@",[dict objectForKey:@"email"]);
                                        NSLog(@"%@",[dict objectForKey:@"password"]);
                                        [altSuccess dismissViewControllerAnimated:YES completion:nil];
                                    }];

        [altSuccess addAction:okSuccess];

        [self presentViewController:altSuccess animated:YES completion:nil];

Its works fine...

5
On

You want to use the Swift enum for the action style like this:

let alertAction2 = UIAlertAction(title: "My Alert", style: .Default)
        {(action:UIAlertAction) -> Void in
            // do something with action
            return
        }

Or more concisely:

let alertAction2 = UIAlertAction(title: "My Alert", style: .Default)
        {action in
            // do something with action
        }