• UIAlertView

    25 February 2015

    Alert views are pop-up views that appear over the current view on the iPhone.

    Creating and showing an alert (ARC compatible):

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        // optional - add more buttons:
        [alert addButtonWithTitle:@"Yes"];
        [alert show];
    

    For non-ARC (retain/release) projects, you must autorelease the alert view:

        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Really reset?" message:@"Do you really want to reset this game?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] autorelease];
        // optional - add more buttons:
        [alert addButtonWithTitle:@"Yes"];
        [alert show];
    

    If you add the UIAlertViewDelegate protocol to your controller, you can also add the following method which is called after the user dismisses the alert view:

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {
            // do stuff
        }
    }
    

    Button indices start at 0 (for the cancelButton specified in the alloc/init), and go up by 1 for each addButtonWithTitle call you add. If you have a lot of alerts, your didDismiss method can keep track of which one is being dismissed if you add the setTag call to the alert initialization: [alert setTag:23];

        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"I'm sorry Dave, I'm afraid I can't do that." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [alert setTag:12];
        [alert show];
    
    ... later ...
    
    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        if ([alertView tag] == 12) {    // it's the Error alert
            if (buttonIndex == 0) {     // and they clicked OK.
                // do stuff
            }
        }
    }
    

    Alert Views With Text Fields

    Alert views can display an optional text input field. This may be useful for asking the user for a username or password. The type of field to display is specified with the alert's alertViewStyle property:


    UIAlertViewStylePlainTextInputUIAlertViewStyleSecureTextInputUIAlertViewStyleLoginAndPasswordInput

     
    IAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:@"The amount?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        UITextField * alertTextField = [alert textFieldAtIndex:0];
        alertTextField.keyboardType = UIKeyboardTypeNumberPad;
        [alertTextField setTextAlignment:NSTextAlignmentCenter];
        [alert show];
        [alert release];
     
        UIAlertView *alert = [[UIAlertView alloc> initWithTitle:@"Username:" message:@"Please enter your username:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        alert.tag = 12;
        
        [alert addButtonWithTitle:@"Go"];
        [alert show];
    }
    

    Then in the delegate method, use the alertView's textFieldAtIndex method to get the text field object. For plain and secure text input styles, the text field is at index 0. For the LoginAndPasswordInput style, the login field is index 0 and the password field is index 1.

    - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if (alertView.tag == 12) {
            if (buttonIndex == 1) {
                UITextField *textfield = [alertView textFieldAtIndex:0];
                NSLog(@"username: %@", textfield.text);
            }
        }
    }


Comments

Comments closed on this post.