In UIAlertView Example, I have explained how to show alert dialogs in iOS. Below are the examples covered in the article.
1). Simple UIAlertview Example
2). UIAlertView Example with User input
1). Simple UIAlertView Example
We can create a simple alert using UIAlertView class.
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"OK Dailog" message:@"This is OK dialog" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; [alert addButtonWithTitle:@"GOO"]; [alert show];
Above code produces the output as shown in the below image.
To handle the button events, we need to add UIAlertViewDelegate protocol to your controller.
@interface ViewController : UIViewController<UIAlertViewDelegate> { }
Add below delegate method to controller
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"Button Index =%ld",buttonIndex); if (buttonIndex == 0) { NSLog(@"You have clicked Cancel"); } else if(buttonIndex == 1) { NSLog(@"You have clicked GOO"); } }
Note: Cancel Button has the buttonIndex value “0”.
2). UIAlertView example with user input
We can take input from user using UIAlertView by changing the style. UIAlertview has the following styles.
typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0, UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, UIAlertViewStyleLoginAndPasswordInput };
2.1 UIAlertView Example with Text Input
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Username" message:@"Enter your username" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert addButtonWithTitle:@"Login"]; [alert show];
User entered input values are read using textFieldAtIndex method.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"Button Index =%ld",buttonIndex); if (buttonIndex == 1) { //Login UITextField *username = [alertView textFieldAtIndex:0]; NSLog(@"username: %@", textfield.text); } }
3.2 UIAlertView with Password input field.
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Password" message:@"Enter Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; alert.alertViewStyle = UIAlertViewStyleSecureTextInput; [alert addButtonWithTitle:@"Login"]; [alert show];
3.3 UIAlertView with Login & password input fields
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Login" message:@"Enter Username & Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alert addButtonWithTitle:@"Login"]; [alert show];
Delegate method for handling button events.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { UITextField *username = [alertView textFieldAtIndex:0]; NSLog(@"username: %@", username.text); UITextField *password = [alertView textFieldAtIndex:1]; NSLog(@"password: %@", password.text); } }
If View controller has multiple dialogs, then we can differentiate them using tag property in UIAlertViewDelegate protocol method.
For that, you need set tag property value.
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"First Dailog" message:@"This is First dialog" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; alert.tag = 1234; [alert addButtonWithTitle:@"OK"]; [alert show]; UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Second Dailog" message:@"This is Second dialog" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; alert.tag = 3456; [alert addButtonWithTitle:@"GO"]; [alert show];
You can filter the dialogs in delegate method using tag property.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { if (alertView.tag == 1234) { //First Dialog if (buttonIndex == 1) //Clicked OK { } } else if (alertView.tag == 3456) //Second Dialog. { if (buttonIndex == 1) //Clicked OK { } } }
Reference:Apple Documentation
The post UIAlertView Example in iOS appeared first on hayaGeek.