One of the nice things about Apple SDK classes is that they tend to be layered. There's usually quite a bit that you can do as soon as you init a class. However, if you want to dig deeper, you can often enjoy additional complexities that can give you more control over exactly how a class looks and what it does. This is the case with UIAlertView.
In my previous discussion of UIAlertView I talked about how to create a UIAlertView object and fill it with some basic information. Now, I'm ready to talk about some of the intricacies.
Adding Buttons
You can modify a UIAlertView by adding buttons to it. Each new button will appear to the right of previous buttons, starting with the cancel button. You can either add these additional buttons as part of your UIAlertView init message or you can use the addButtonWithTitle: method.
[alertTest addButtonWithTitle:@"Button #2"];
You can even retrieve the titles of buttons by looking them up in an array. numberOfButtons tells you how many buttons a UIAlertView has, while cancelButtonIndex tells you where the cancel button is, and buttonTitleAtIndex: returns the title for a specific button number.
All told, this can not only let you create UIAlertViews that offer several options, but it can also give you access to considerable dynamism, since you can look up which button is which at run time.
Delegating
Of course, if you don't do anything else, you'll find that each UIAlertView button just cancels the UIAlertView ... which isn't a very useful result for a multiple button interface. That's where delegation comes in. You can set a delegate as part of your UIAlertView init, or by using the delegate property. Afterward you need to respond to one very critical message:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
After alertView:clickButtonAtIndex: is run, the UIAlertView will automatically be dismissed. It's what happens before that which matters. You can use the method to modify instance variables, change your views, or do anything else that's appropriate for your program.
The following code shows the complete setup for a multi-button UIAlertView that responds to delegation--and some simple delegation results.
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:@"This is a Test"
message:@"This is the message contained with a UIAlertView"
delegate:self
cancelButtonTitle:@"Button #1"
otherButtonTitles:nil];
[alertTest addButtonWithTitle:@"Button #2"];
[alertTest show];
[alertTest autorelease];
messageLabel.text = @"You have not yet pushed a button.";
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
messageLabel.text = [NSString stringWithFormat:
@"You clicked button #%i",buttonIndex +1];
}
That's it for this time. If you have a specific class that you'd like to see covered because we were not able to get to it in the book, let me know in the comments below, and I'll keep it in mind for my next article, which should be a couple of weeks' hence.