Alerts

April 26th, 2009 by Chris Leave a reply »

uialertviewAlerts are widely used in many ways. Very often they are shown on first run to say hello. Also to inform you about something necessary, important. They ask you to wait several seconds, display warnings, errors.

In fact they look just great, stunning and they suits almost any game / application interface. End user might think that we did a lot of hard work to display something so polished like this while it actually only few lines of code.

To display an alert (UIAlertView) like on the screenshot above you need to add:

	UIAlertView *simpleAlert = [[UIAlertView alloc] initWithTitle:@"Alert's title" message:@"This is an alert's message." delegate:self cancelButtonTitle:@"and the" otherButtonTitles:@"buttons",nil];
	[simpleAlert show];
	[simpleAlert release];

You can show the alert wherever you like in you application for example in viewDidLoad method or after pressing button. If you alert has more than two buttons they will be displayed in a single column, not row:

alertview3buttons

As long as you won’t add an UIAlertViewDelegate protocol to your class and implement  alertView:clickedButtonAtIndex: method, pressing any button will only dismiss your alert. Protocol is added in header file of your class:

@interface MyView : UIViewController <UIAlertViewDelegate> {

If you have more than one protocol separate them with commas: <oneProtocol, theOtherProtocol>. Now in implementation (.m) file add this method:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 1) {
		// do something
	}
	else if (buttonIndex == 2) {
		// do something
	}
}
As usuall first button (in 99% cancel button) has index = 0. If you have more than one “complex alert” and each of them has different roles it’s good to use tags for them and check in alertView:clickedButtonAtIndex: what value an alert’s tag has which called this method.
	UIAlertView *complexAlert = [[UIAlertView alloc] initWithTitle:@"Complex Alert" message:@"Select an action" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Sure",@"Not sure",nil];
	complexAlert.tag = 1;
	[complexAlert show];
	[complexAlert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if ([alertView tag] == 1) {
		if (buttonIndex == 1) {
			// do something
		}
		else if (buttonIndex == 2) {
			// do something
		}
	}
	else {
		// do something
	}
}
That’s almost all you need to know about alerts but there is one more interesting thing. If you would like to dismiss an alert after several seconds – for example a welcome screen or activate a default option, follow this example:
	autoAlertView = [[UIAlertView alloc] initWithTitle:@"Auto-dismissed Alert" message:@"This alert will be dismissed in 5 seconds." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
	userDismissed = NO;
	[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(dismissAlert) userInfo:nil repeats:NO];
	[autoAlertView show];
-(void)dismissAlert { if (userDismissed) return; [autoAlertView dismissWithClickedButtonIndex:0 animated:YES]; [autoAlertView release]; }
As usuall you can download a sample project here:
xcodeproj
Advertisement

5 comments

  1. Isreal Evans says:

    Sites pour guitaristes les references sites web topliste classement cours de guitare
    classement topliste classement sites guitare apprendre la guitare évolutivement à la guitare, la méthode complète pros réalisés par un prof de guitare
    expériementé auteur de nombreuses méthodes pour étudier la guitare.
    Des cours de guitare complets et progressifs avec vidéos pour rendre l’aprpentissage encore plus facile et ludique.
    Vous allez découvrir pas à pas comment débuter à la guitare, à placer les différentes notes de musiques aux différents enrroits du manche de votre guitare,
    vous allez également étudier les accords majeurs et mineurs à la guitare.
    Des cours de guitare réalisés par un véritable guitariste pour les guitaristes qui veulent apprendre sérieusement et réellement la guitare dans les meilleures conditions.
    Une méthode guitare complète ludique et terriblement efficace pour jouer rapidement vos morceaux de musique favoris, seul(e) ou en groupe.
    pages guitaristes
    Inscrivez vos sites

  2. Precle says:

    I am impressed, I must say. Really rarely do I discovered a blog thats both educational and entertaining, and let me tell you, you have hit the nail on the head. Your blog is important; the issue is something that not enough people are speaking intelligently about. Im really happy that I stumbled across this in my search for something relating to this.

  3. Rosemary Bastick says:

    Or just use Say it & Mail it’ and tap on the camera icon to get a picture from the camera roll or take a new picture to be included in the email.

  4. Hello.This post was really interesting, particularly since I was investigating for thoughts on this matter last Thursday.

Leave a Reply