Sunday 11 August 2013

UIAlertViewStyle property of UIAlertView

Hi Guys,

Hope everybody doing well. Recently I got a requirement in one of my project to create a UIAlertView with "User Name" and "Password" fields. Prior to iOS 5.0, to achieve this functionality we need to subclass UIAlertView and add UITextField as subviews to the UIAlertView.

Initially I thought of subclassing UIAlertView to achieve this functionality, but I came to know that from iOS 5.0 Apple has added a new property "UIAlertViewStyle alertViewStyle" to UIAlertView class.

Following are the various options available for UIAlertViewStyle property :


UIAlertViewStyleDefaultUIAlertViewStyleSecureTextInputUIAlertViewStylePlainTextInputUIAlertViewStyleLoginAndPasswordInput

Now, lets create a single view application and setup the view layout as show below :


Set tags to 1001 to "Default, 1002 to "Secure", 1003 to "Plain" and 1004 to "Login & Password" buttons using xib file.

Add following action method code to your implementation file :

- (IBAction)showAlert:(id)sender
{
    UIAlertView *alertView = [[UIAlertView alloc] init];
    
    switch (((UIButton *)sender).tag)
    {
        // Default Alert View
        case 1001:
            
            alertView.title = @"Default Alert View";
            alertView.message = @"UIAlertViewStyleDefault";
            [alertView addButtonWithTitle:@"OK"];
            
            break;
           
        // Secure Alert View
        case 1002:
            
            alertView.title = @"Secure Alert View";
            alertView.message = @"UIAlertViewStyleSecureTextInput";
            alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
            [alertView addButtonWithTitle:@"OK"];
            [alertView addButtonWithTitle:@"Cancel"];
            
            break;
            
        // Plain Alert View
        case 1003:
            
            alertView.title = @"Plain Alert View";
            alertView.message = @"UIAlertViewStylePlainTextInput";
            alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
            [alertView addButtonWithTitle:@"OK"];
            [alertView addButtonWithTitle:@"Cancel"];

            break;
          
        // Login ALert View
        case 1004:
            
            alertView.title = @"Login Alert View";
            alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
            [alertView addButtonWithTitle:@"OK"];
            [alertView addButtonWithTitle:@"Cancel"];

            break;
    }
    
    [alertView show];
}


Now connect this action to all buttons in xib file to "Touch up inside" UIControlEvent. 

Run application and following is the output :

Default Style :


Secure Style :

Plain Style :

Login & Password :


This saved a lot of time for me. No need to subclass UIAlertView anymore to create a login view. Hope this will save time of your's too.



 

1 comment:

  1. The Beginner's Guide to Baccarat - FEBCasino
    Betting 바카라 사이트 on 1xbet korean the outcome of a race is 카지노 easy if you know how to calculate how many bets you can make. The basic rules for the game are:; You must win; You win;

    ReplyDelete