Tag Archives: UIAlertView

How to create alert views and action sheets in iOS 8

Screen Shot 2015 09 28 at 23 37 12

Up until iOS 7 we could use the UIAlertView and UIActionSheet classes to bring up alerts and action sheets respectively. As of iOS 8 those classes are deprecated. Although they still work, we’re encouraged to use the UIActionController class instead. Here’s how to use it.

The main difference is that both of the old classes have been converged into one, so now we simply tell the alert controller how we want to bring up the information.

While the old classes used a delegate protocol so we could react to the outcome of user interactions, the UIActionController class uses completion blocks, making our coding efforts a little easier. All we have to do is create an action, specify things like title, subtitle and appearance, and add a block that shall be executed upon completion.

Here are three examples.

Continue reading





What are the button indexes in a three-button UIAlertView

iOS Simulator Screen shot 30 Dec 2013 09.50.43

The cancel button is always index 0, and all other buttons in the array start at index 1 counting up.

For completion, here’s how to create it:

- (IBAction)showAlert:(id)sender {
    
    // create an alert view with three buttons
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Three Button Alert" message:@"This is an alert view with more than two buttons. Will it work?" delegate:self cancelButtonTitle:@"No Way (0)" otherButtonTitles:@"Perhaps (1)", @"Definitely! (2)", nil];
    [alertView show];
}

And here’s how to react to it:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    switch (buttonIndex) {
        case 0:
            NSLog(@"That was button at index 0");
            break;
            
            case 1:
            NSLog(@"That was button at index 1");
            break;
            
            case 2:
            NSLog(@"That was button at index 2");
            break;
            
            case 3:
            NSLog(@"We don't have a fourth button");
            break;
            
        default:
            break;
    }
}

This is a delegate method, so the reacting class needs to conform to the UIAlertView Protocol like so:

@interface ViewController : UIViewController <UIAlertViewDelegate>




How to react to multiple UIAlertViews

If your class creates more than one UIAlertView, then you need a way to react to each of those accordingly. Problem is, you may only have one delegate that listens to all alerts at any given time.

You could of course create a separate class for each alert view, but that’s a bit overkill. Instead, the UIView’s tag property comes in handy with which you can identify a view at runtime (and UIAlertView inherits from UIView).

tag is an integer so you can only give it whole numbers, like so:

- (void)alertOne {
    UIAlertView *infoMessage;
    infoMessage = [[UIAlertView alloc]
                   initWithTitle:@"Title 1" message:@"Message 1 goes here."
                   delegate:self cancelButtonTitle:@"Exciting stuff!" otherButtonTitles:@"Nice!", nil];
    infoMessage.alertViewStyle = UIAlertViewStyleDefault;
    infoMessage.tag = 1;
    [infoMessage show];
}

- (void)alertTwo {
    UIAlertView *infoMessage;
    infoMessage = [[UIAlertView alloc]
                   initWithTitle:@"Title 2" message:@"Message 2 goes here."
                   delegate:self cancelButtonTitle:@"Exciting stuff!" otherButtonTitles:@"Nice!", nil];
    infoMessage.alertViewStyle = UIAlertViewStyleDefault;
    infoMessage.tag = 2;
    [infoMessage show];
}

In your delegate method you can now ask which tag is set when those buttons are pressed:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    // reacting to tag 1
    if (alertView.tag == 1) {
        if (buttonIndex == 0) {
            NSLog(@"User pressed first button");
        } else if (buttonIndex == 1) {
            NSLog(@"User pressed second button");
        }
    }
    
    // reacting to tag 2
    if (alertView.tag == 2) {
        if (buttonIndex == 0) {
            NSLog(@"User pressed first button");
        } else if (buttonIndex == 1) {
            NSLog(@"User pressed second button");
        }
    }
}

See also:





How to react to a UIAlertView

An alert view has the clickedButtonAtIndex property. The first clicked button is 0, the next one 1, and so forth. If you conform to the UIAlertViewDelegate protocol you can implement the following method and react to each pressed button accordingly, or simply call a method:

You can also access text fields via the textFieldAtIndex property.





How to handle an error while loading a UIWebView

If you’re conforming to the UIWebViewDelegate protocol the webView:didFailLoadWithError: method gets triggered when there has been a problem.
Call it like this and display an error message (in this case a UIAlertView):

This method is also a good opportunity to stop any loading animations you’ve got going.





How to create an Alert View

Here’s how we can create a simple pop-up window via the UIAlertView instance. It can have a title, some info text and several buttons (OK, Cancel, etc). We’ll only deal with one button and not worry about how to read out which button value has been pressed.

This is what a UIAlertView looks like

And here’s how we build this on Objective-C:

UIAlertView *infoMessage;
infoMessage = [[UIAlertView alloc]
               initWithTitle:@"Image Switcher 1.0" message:@"This app switches 
               between several UIIMageViews and displays a new picture in each one. 
               It also uses the segmented control thingy. 
               Check out www.versluis.com for more goodies." 
       delegate:self cancelButtonTitle:@"Exciting stuff!" otherButtonTitles:nil];
infoMessage.alertViewStyle = UIAlertViewStyleDefault;
[infoMessage show];

This is quite a long block of code so here are the steps:

  • create an instance of the UIAlertView
  • allocate and initiate it with parameters Title, Message, and an array of buttons (cancel and nil if you’re only using a single button to dismiss the view)
  • give the view a style (mandatory)
  • display the view

You can add a \n for a line break inside the window.