"

iPhone User Alerts

Thursday, April 2, 2009 @ 09:04 AM
posted by Geppy Parziale

In Cocoa, you can send alert to the user sending NSAlert class. In Cocoa Touch, you can achieve the same result using the UIAlertView and UIActionSheet classes.

UIAlertView

UIAlertView allows you to send messages to the user. To use an alert in your class, it should be conform to the UIAlertViewDelegate protocol. In this way, when one of the alert buttons is pressed, the following method can be called:

1
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

To launch an alert is very simple. In your delegate class, you call the following method:

1
2
3
4
5
- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
            delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles,...

As an example, you can call in your class the following lines of code:

1
2
3
4
5
6
7
8
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@”Alert”
                                                message:@”This is an alert”
                                                delegate:self
                                  cancelButtonTitle:@”Cancel”
                                otherButtonTitles:nil];
[alert show];
[alert release];

Once the user has pressed the Cancel button, the previous delegate method -alertView:clickedButtonAtIndex is called and the user answer can be processed. The button index depends on the button position and the index starts with 0.

UIActionSheet

UIActionSheet works in the same way. In this case, the action sheet is needed to ask the user to take a decision. The action sheet can be visualized in a view, from a tab bar or from a navigation bar.
If your class uses an action sheet, it must be conform to the UIActionSheetDelegate protocol. In this way, it will be able to call

1
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

and process the user action.
To launch an action is very simple too. In your delegate class, call the following method:

1
2
3
4
5
 -(id)initWithTitle:(NSString *)title
             delegate:(<UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
 otherButtonTitles:(NSString *)otherButtonTitles, ...

For example, you can do the following:

1
2
3
4
5
6
7
8
UIActionSheet *action;
action = [[UIActionSheet alloc] initWithTitle:@"Action"
                                                      delegate:self
                                        cancelButtonTitle:@"Cancel"
                                destructiveButtonTitle:@"OK"
                                       otherButtonTitles:nil];
[action showInView:self];
[action release];

Example

Let’s make a small iPhone application using an alert view.
Open Xcode and create a Window-based application. Call it SimpleAlert. Open the SimpleAlertAppDelegate.h file and change it as it follows:

1
2
3
4
5
6
#import
@interface SimpleAlertAppDelegate : NSObject  {
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

Notice the UIAlertViewDelegate protocol.
Now, change the SimpleAlertAppDelegate.m file. In the -applicationFinishLaunching method add the following:

1
2
3
4
5
6
7
8
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                                    message:@"This is an alert"
                                                                   delegate:self
                                                     cancelButtonTitle:nil
                                                     otherButtonTitles:@"one",@"two",@"three",nil];
[alert show];
[alert release];
[window makeKeyAndVisible];

Then, implement the following method:

1
2
3
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"You pressed button %d",buttonIndex+1);
}

Build and Run. The following alert will appear on your iPhone.

When you press one of the button a message should appear in the console.

Have a nice coding.
++Geppy

Post to Twitter Post to Facebook

Leave a Reply