Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to find a clear explanation about swift alert (UIAlertController)?

Tags:

Couldn't find a clear and informative explanation for this.

like image 746
Nikita Kurtin Avatar asked May 16 '15 13:05

Nikita Kurtin


People also ask

How do I see AppDelegate alerts?

@OrkhanAlizade create a ViewController , put your code into the ViewControllers viewDidAppear method, and in your AppDelegate , set that ViewController as the windows rootViewController (and also don't forget to create the window itself). @DánielNagy it works! Thank you!

How do I show alert view in Swift?

Code to display Alert Box with OK and Cancel Buttons, let alertController = UIAlertController(title: "Alert title", message: "Message to display", preferredStyle: . alert) // Create OK button.

What is a UIAlertController?

An object that displays an alert message to the user.

How do I show custom alerts in Swift?

storyboard and add two buttons on View controller Scene. These buttons are used to display two types of alert i.e single button and two button alert. Select Cocoa Touch Class from Source and click Next. This class will handle all Custom Alert logic and user actions.


1 Answers

After searching a while on a subject I didn't find a clear explanation , even in it's class reference UIAlertController Reference

It is ok, but not clear enough for me.

So after collecting some peaces I decided to make my own explanation (Hope it helps)

So here it goes:

  1. UIAlertView is deprecated as pointed out : UIAlertView in Swift
  2. UIAlertController should be used in iOS8+ so to create one first we need to instantiate it, the Constructor(init) gets 3 parameters:

2.1 title:String -> big-bold text to display on the top of alert's dialog box

2.2 message:String -> smaller text (pretty much explains it's self)

2.3 prefferedStyle:UIAlertControllerStyle -> define the dialog box style, in most cases: UIAlertControllerStyle.Alert

  1. Now to actually show it to the user, we can use showViewController or presentViewController and pass our alert as parameter

  2. To add some interaction with a user we can use:

4.1 UIAlertController.addAction to create buttons

4.2 UIAlertController.addTextField to create text fields

Edit note: code examples below, updated for swift 3 syntax

Example 1: Simple Dialog

@IBAction func alert1(sender: UIButton) {      //simple alert dialog     let alert=UIAlertController(title: "Alert 1", message: "One has won", preferredStyle: UIAlertControllerStyle.alert);     //show it     show(alert, sender: self); } 

Example 2: Dialog with one input textField & two buttons

@IBAction func alert2(sender: UIButton) {     //Dialog with one input textField & two buttons     let alert=UIAlertController(title: "Alert 2", message: "Two will win too", preferredStyle: UIAlertControllerStyle.alert);     //default input textField (no configuration...)     alert.addTextField(configurationHandler: nil);     //no event handler (just close dialog box)     alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.cancel, handler: nil));     //event handler with closure     alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in         let fields = alert.textFields!;         print("Yes we can: "+fields[0].text!);     }));     present(alert, animated: true, completion: nil); } 

Example 3: One customized input textField & one button

@IBAction func alert3(sender: UIButton) {    // one input & one button    let alert=UIAlertController(title: "Alert 3", message: "Three will set me free", preferredStyle: UIAlertControllerStyle.alert);      //configured input textField     var field:UITextField?;// operator ? because it's been initialized later     alert.addTextField(configurationHandler:{(input:UITextField)in         input.placeholder="I am displayed, when there is no value ;-)";         input.clearButtonMode=UITextFieldViewMode.whileEditing;         field=input;//assign to outside variable(for later reference)     });     //alert3 yesHandler -> defined in the same scope with alert, and passed as event handler later     func yesHandler(actionTarget: UIAlertAction){         print("YES -> !!");         //print text from 'field' which refer to relevant input now         print(field!.text!);//operator ! because it's Optional here     }     //event handler with predefined function     alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: yesHandler));      present(alert, animated: true, completion: nil);  } 

Hope It helps, and good luck ;-)

like image 80
Nikita Kurtin Avatar answered Oct 14 '22 14:10

Nikita Kurtin