Aug 4, 2010

iPhone - UIAlertView






UIAlertView is not a common component. Use alert to display message is not a good design, because display UIAlertView will lock the screen. User need press "OK" Button to resume the operation.

In most case, UIAlertView used in map related application such as Google maps. In these application, use UIAlertView to alert user application will get the current location. Application get user current location is a importance message, because user may in a private place. Don't want other people know this place. So application use UIAlertView to display this importance message.


UIAlertView component use method is very simple. Here is a way to display a "Message Box" and one "OK" button in this box.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];
[alert release];

It is a screen shot:


In this example, UIAlertViewdoes not have delegate, because this message only display message. Do not need any call back method.

UIAlertView at least have one button. This a Cancel Button. Cancel Button title can be change, but cannot remove this button.

You can also add more button into UIAlertView. Just add a NSString into otherButtonTitles. Like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];

We are added 3 button into UIAlertView.


You can run some code when user press any button. Just add an UIAlertViewDelegate protocol in some class, such as UIViewController:

ViewController.h
#import <uikit/uikit.h>
@interface ViewController : UIViewController <UIAlertViewDelegate> {
    // code
}
@end

Add below method in UIViewController
ViewController.m
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    // code
}

You can use below method to determine which button was pressed.
- (void)loadView {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
    [alert show];
    [alert release];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
        NSLog(@"Cancel Button Pressed");
        break;
        case 1:
        NSLog(@"Button 1 Pressed");
        break;
        case 2:
        NSLog(@"Button 2 Pressed");
        break;
        case 3:
        NSLog(@"Button 3 Pressed");
        break;
        default:
    }
}


2 comments:

avg free download said...

duuuuuuude, wassssssup

naklejki said...

I really enjoy reading Your page, I have added it to favourites. I believe that You will keep up with good work, your article was very engrossing. I hope you have a great day! Naklejki scienne, naklejki na sciany

Post a Comment

Twitter Delicious Facebook Digg Google Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Hostgator Coupon Code