Creating Alert Views

- initWithTitle: message: delegate: cancelButtonTitle: otherButtonTitles:

Convenience method for initializing an alert view.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView show];
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
alertView.show()

Output

Setting Properties

- delegate

The receiver’s delegate or nil if it doesn’t have a delegate.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
NSLog(@"AlertViewDelegate :%@");
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
println("AlertViewDelegate :\(alertView.delegate)");

Output

AlertViewDelegate :ViewController: 0x7fb89042f5a0

- alertViewStyle

The kind of alert displayed to the user.Available styles include UIAlertViewStyleDefault, UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, UIAlertViewStyleLoginAndPasswordInput.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
alertView.alertViewStyle = UIAlertViewStyle.Default

- title

The string that appears in the receiver’s title bar.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
NSLog(@"AlertView Title :%@",alertView.title);
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
println("AlertView Title :\(alertView.title)");

Output

2015-07-10 10:31:03.967 Eezy[2570:119253] AlertView Title :Eezy

- message

Descriptive text that provides more details than the title.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
NSLog(@"AlertView Message :%@",alertView.message);
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
println("AlertView Message :\(alertView.message)");

Output

2015-07-10 10:32:07.197 Eezy[2612:119952] AlertView Message :UIAlertView example on eezytutorials.com

- visible

A Boolean value that indicates whether the receiver is displayed (read-only).

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
NSLog(@"AlertView isVisible :%d",alertView.visible);
[alertView show];
NSLog(@"AlertView isVisible :%d",alertView.visible);
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
println("AlertView isVisible :\(alertView.visible)");
alertView.show()
println("AlertView isVisible :\(alertView.visible)");

Output

2015-07-10 10:34:52.492 Eezy[2674:121587] AlertView isVisible :0
2015-07-10 10:34:52.577 Eezy[2674:121587] AlertView isVisible :1
AlertView isVisible :false
AlertView isVisible :true

Configuring Buttons

- addButtonWithTitle:

Adds a button to the receiver with the given title.

Example

UIAlertView *alertView = [[UIAlertView alloc]init];
[alertView addButtonWithTitle:@"Ok"];
let alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")

- numberOfButtons

The number of buttons on the alert view (read-only).

Example

UIAlertView *alertView = [[UIAlertView alloc]init];
[alertView addButtonWithTitle:@"Ok"];
NSLog(@"Number of buttons: %ld",(long)alertView.numberOfButtons);
let alertView = UIAlertView()
alertView.addButtonWithTitle("Ok")
println("Number of buttons: \(alertView.numberOfButtons)")

Output

2015-07-10 10:56:36.950 Eezy[3027:132749] Number of buttons: 1
Number of buttons: 1

- buttonTitleAtIndex:

Returns the title of the button at the given index.

Example

UIAlertView *alertView = [[UIAlertView alloc]init];
alertView.title = @"Eezy";
alertView.message = @"UIAlertView example on eezytutorials.com";
[alertView addButtonWithTitle:@"Ok"];
[alertView addButtonWithTitle:@"Cancel"];
NSLog(@"buttonTitleAtIndex: %@",[alertView buttonTitleAtIndex:1]);
alertView.delegate = self;
[alertView show];
let alertView = UIAlertView()
alertView.title = "Eezy";
alertView.message = "UIAlertView example on eezytutorials.com";
alertView.addButtonWithTitle("Ok")
alertView.addButtonWithTitle("Cancel")
println("buttonTitleAtIndex: \(alertView.buttonTitleAtIndex(1))")
alertView.delegate = self

Output

2015-07-10 11:06:08.119 Eezy[3229:137838] buttonTitleAtIndex: Cancel
buttonTitleAtIndex: Cancel

- textFieldAtIndex:

Returns the text field at the given index.

Example

UIAlertView *alertView = [[UIAlertView alloc]init];
alertView.title = @"Eezy";
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView addButtonWithTitle:@"Ok"];
NSLog(@"textFieldAtIndex: %@",[alertView textFieldAtIndex:0]);
alertView.delegate = self;
[alertView show];
let alertView = UIAlertView()
alertView.title = "Eezy";
alertView.addButtonWithTitle("Ok")
alertView.addButtonWithTitle("Cancel")
alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput
println("textFieldAtIndex: \(alertView.textFieldAtIndex(0))")
alertView.delegate = self

Output

2015-07-10 11:13:13.873 Eezy[3440:142103] textFieldAtIndex: <_UIAlertControllerTextField: 0x7f81e3f62cb0; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = >
textFieldAtIndex: Optional(<_UIAlertControllerTextField: 0x7f81e3c68c50; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = >)

- cancelButtonIndex

The index number of the cancel button.

Example

UIAlertView *alertView = [[UIAlertView alloc]init];
alertView.title = @"Eezy";
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView addButtonWithTitle:@"Ok"];
alertView.cancelButtonIndex = 0;
NSLog(@"cancelButtonIndex: %ld",(long)[alertView cancelButtonIndex]);
alertView.delegate = self;
[alertView show];
let alertView = UIAlertView()
alertView.title = "Eezy";
alertView.addButtonWithTitle("Ok")
alertView.addButtonWithTitle("Cancel")
alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput
alertView.cancelButtonIndex = 1
println("cancelButtonIndex: \(alertView.cancelButtonIndex)")
alertView.delegate = self

Output

2015-07-10 11:16:40.619 Eezy[3536:143994] cancelButtonIndex: 0
cancelButtonIndex: 1

- firstOtherButtonIndex

The index of the first other button (read-only).

Example

UIAlertView *alertView = [[UIAlertView alloc]init];
alertView.title = @"Eezy";
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView addButtonWithTitle:@"Ok"];
[alertView addButtonWithTitle:@"Cancel"];
alertView.cancelButtonIndex = 0;
NSLog(@"firstOtherButtonIndex: %ld",(long)[alertView firstOtherButtonIndex]);
alertView.delegate = self;
[alertView show];
let alertView = UIAlertView()
alertView.title = "Eezy";
alertView.addButtonWithTitle("Ok")
alertView.addButtonWithTitle("Cancel")
alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput
alertView.cancelButtonIndex = 1
println("firstOtherButtonIndex: \(alertView.firstOtherButtonIndex)")
alertView.delegate = self

Output

2015-07-10 11:18:22.133 Eezy[3575:144899] firstOtherButtonIndex: -1
firstOtherButtonIndex: -1

Displaying

- show

Displays the receiver using animation.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView show];
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
alertView.show()

Dismissing

- dismissWithClickedButtonIndex:animated:

Dismisses the receiver, optionally with animation.

Example

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Eezy" message:@"UIAlertView example on eezytutorials.com" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView show];
[alertView dismissWithClickedButtonIndex:0 animated:YES];
let alertView = UIAlertView(title: "Eezy", message: "UIAlertView example on eezytutorials.com", delegate: self, cancelButtonTitle: "OK")
alertView.show()
alertView.dismissWithClickedButtonIndex(0, animated: true)

Advertisements