iOS display Alert using UIAlertView

With the help of UIAlertView you can notify the user, ask the user for confirmation or some type of input. There are various types of alerts depending on the alert view style. We are going to review how to create all the different alert styles that are available and then using the UIAlertViewDelegate to find out which button was pressed and the text that was input by the user.
iOS UIAlertView Example
iOS Default Alert View Style
iOS Plain Text Alert View Style
iOS Secured Text Alert View Style
iOS Login and Password Alert View Style
 

Interface file for the view controller - MyAlertViewViewController.h

#import <UIKit/UIKit.h>

@interface MyAlertViewViewController : UIViewController <UIAlertViewDelegate>

@property (nonatomic, strong) UIButton *myButton;

@end

Implementation file for the view controller - MyAlertViewViewController.m

#import "MyAlertViewViewController.h"

@interface MyAlertViewViewController ()

@end

@implementation MyAlertViewViewController

@synthesize myButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //origin and size for our buttons
    CGRect myFrame = CGRectMake(10.0f, 10.0f, 300.0f, 30.0f);
    for (int i=0; i<4; i++) {
        
        //create the button and set the frame
        self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self.myButton setFrame:myFrame];
        
        //set the button title
        NSString *buttonTitle = @"Default Alert";
        switch (i) {
            case 1:
                buttonTitle = @"Plain Text Input Alert";
                break;
            case 2:
                buttonTitle = @"Secure Text Input Alert";
                break;
            case 3:
                buttonTitle = @"Login and Password Input Alert";
                break;
                
            default:
                break;
        }
        [self.myButton setTitle:buttonTitle
                       forState:UIControlStateNormal];
        
        //set the action method when the button is clicked
        [self.myButton addTarget:self
                          action:@selector(displayMyAlert:)
                forControlEvents:UIControlEventTouchUpInside];
        //set button tag for identification
        [self.myButton setTag:i+1];
        //add the button to the view
        [self.view addSubview:self.myButton];
        //move the origin for the next button
        myFrame.origin.y += 35.0f;
        
    }
    
    
}

- (void) displayMyAlert:(UIButton *) button{
    
    NSLog(@"Button was clicked, lets display our alert view");
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:button.currentTitle
                                        message:@"Put whatever message you want"
                                        delegate:self
                                        cancelButtonTitle:@"Cancel"
                                        otherButtonTitles:@"Ok", nil];
    
    //based on the button set the alert view style
    switch (button.tag) {
        case 2:
            //An alert that allows the user to enter text
            [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
            break;
        case 3:
            //An alert that allows the user to enter text. The text field is obscured
            [alertView setAlertViewStyle:UIAlertViewStyleSecureTextInput];
            break;
        case 4:
            //An alert that allows the user to enter a login identifier and password
            [alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
            break;
            
        default:
            break;
    }
    
    //display our alert
    [alertView show];
    
}

- (void) alertView:(UIAlertView *)alertView
                clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    //which button was pressed in the alert view
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"Ok"]){
        NSLog(@"Ok button was pressed.");
    }
    else if ([buttonTitle isEqualToString:@"Cancel"]){
        NSLog(@"Cancel button was pressed.");
    }
    
    //which alert view it is?
    NSString *alertTitle = [alertView title];
    NSLog(@"%@", alertTitle);
    
    // If plain text or secure text
    if ([alertView alertViewStyle] == UIAlertViewStylePlainTextInput ||
        [alertView alertViewStyle] == UIAlertViewStyleSecureTextInput)
        //find the text field by index and then find its value
        NSLog(@"Text: %@", [[alertView textFieldAtIndex:0] text]);
    
    // Login and password, they have 2 text field at index 0 and 1
    else if ([alertView alertViewStyle] == UIAlertViewStyleLoginAndPasswordInput)
    {
        //find the text field by index and then find its value
        NSLog(@"Login: %@", [[alertView textFieldAtIndex:0] text]);
        //find the text field by index and then find its value
        NSLog(@"Password: %@", [[alertView textFieldAtIndex:1] text]);
    }
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Reference