Skip to content
rayastar edited this page Feb 7, 2015 · 1 revision

1.1 Designated initializer. viewDidAppear: Notifies the view controller that its view was added to a view hierarchy.

- (void) viewDidAppear:(BOOL)paramAnimated
{
    UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Alert"
                              message:@"You've been delivered an alert"
                              delegate:nil
                              cancelButtonTitle:@"Cancel"
                              otherButtonTitles:@"Ok", @"Other button", nil];
      [alert show];
}

standart alert view

1.2 As an example, let’s display an alert view to the user and ask whether she would like to visit a website in Safari after having pressed a link to that website available in our UI. We will display two buttons on our alert view: Yes and No. In our alert view delegate, we will detect which button she tapped on and will take action accordingly.

- (NSString *) yesButtonTitle {
    return @"Yes";
}

- (NSString *) noButtonTitle {
    return @"No";
}

Now we need to make sure that we are conforming to the UIAlertViewDelegate protocol in our view controller:

@interface ViewController () <UIAlertViewDelegate>

The next step is to create and display our alert view to the user:

    UIAlertView *aletrshow = [[UIAlertView alloc]
                                initWithTitle:@"Open Link"
                                message:@"Are you sure you want to open this link in Safari?"
                                delegate:self
                                cancelButtonTitle:[self noButtonTitle]
                                otherButtonTitles:[self yesButtonTitle], nil];
    [aletrshow show];
two

Now we need a way to know whether the user selected the Yes or the No option in our alert view. For this, we will need to implement the alertView:clickedButtonAtIn dex: method of our alert view delegate:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:[self yesButtonTitle]]){
        NSLog(@"User pressed the Yes button.");
    }
    else if ([buttonTitle isEqualToString:[self noButtonTitle]]){
        NSLog(@"User pressed the No button.");
    }
}

Alert views can take various styles. The UIAlertView class has a property called alert ViewStyle of type UIAlertViewStyle:

typedef NS_ENUM(NSInteger, UIAlertViewStyle){
    UIAlertViewStyleDefault = 0,
    UIAlertViewStyleSecureTextInput,
    UIAlertViewStylePlainTextInput,
    UIAlertViewStyleLoginAndPasswordInput
};

You can also use an alert view for text entry, such as to ask the user for his credit card number or address. For this, as mentioned before, we need to use the UIAlert ViewStylePlainTextInput alert view style. Here is an example:

- (void) alertTwo
{

    UIAlertView *aletrshow = [[UIAlertView alloc]
                              initWithTitle:@"Credit Card Number"
                              message:@"Please enter your credit card number:"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              otherButtonTitles:@"Ok", nil];

     [aletrshow setAlertViewStyle:UIAlertViewStylePlainTextInput];
    /* Display a numerical keypad for this text field */
    UITextField *textField = [aletrshow textFieldAtIndex:0];
    textField.keyboardType = UIKeyboardTypeNumberPad;

    [aletrshow show];

}
three
Clone this wiki locally