Working with alerts

Alerts are those blue windows that appear in the middle of the screen when your device needs to tell you something, in theory, important. They are very common in iOS and very simple to implement so you should learn how to use them in your iPhone Apps.

A lerts are those blue windows that appear in the middle of the screen when your device needs to tell you something, in theory, important. They are very common in iOS and very simple to implement so you should learn how to use them in your iPhone Apps.

Before you read

I’ll assume you know at least the basics of iPhone Development. If you don’t, i recommend to visit my tutorials for beginners here: http://www.oscarvgg.com/how-to-make-iphone-apps-–-part-1-xcode-suite-and-objective-c/

Setting up the Project

Open up Xcode and hit File->New Project…, Now in the iPhone OS section select “Application” and in the right double click “View-Based Application”.

iPhone apps | Working with alerts image 1

Give your project a name. I’m going to call mine “AlertTut”.

In the left side click Classes and then AlertTutViewController.m.

Ok, what i want to do here is that an alert appear when i open my app and thats it, so i have to find a method called viewDidLoad and uncomment it. This is a built in method that fires up when this view has finished loading. Scroll down in the mentioned class and you’ll find it.

Remove the /* and */ before and after the method.

iPhone Apps | Working with alerts image 2

And we will start adding code right before [super viewDidLoad].

First type:

1
UIAlertView * pop = [[UIAlertView alloc]

With this we are allocating an space of memory for our object. Then we need to specify some attributes of the UIAlertView class.

1
initWithTitle:@"My Alert App"

This is the title that will be displayed in the header of the alert window.

1
message:@"Welcome to my app"

Message is the text that you need to communicate or that is, in someway, relevant to the user.

1
delegate:nil

Delegate is to tell the app what to do after the alert is dismissed. In this case i what to do nothing so i said nil which the equivalent to null un other languages.

1
cancelButtonTitle:@"Thanks!"

This is the text to display in the dismiss button. I’m going to thank for welcoming me.

1
otherButtonTitles:nil];

Here we say the titles for other buttons if you whant to add more, for me is fine with that so i typed nil, and closed finished the statement with ];.

Now we let the alert appear:

1
[pop show];

And just because i’m responsible i’m going to release the object. Remember the rule, “If you create an object, it’s your responsibility to release it”.

1
[pop release];

If you did everything like me, your viewDidLoad method should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)viewDidLoad {
 
	UIAlertView * pop = [[UIAlertView alloc]
				initWithTitle:@"My Alert App"
				message:@"Welcome to my app"
				delegate:nil
				cancelButtonTitle:@"Thanks!"
				otherButtonTitles:nil];
 
	[pop show];
	[pop release];
 
    [super viewDidLoad];
}

Now run the app. And there is our alert.

iPhone apps | Working with alerts image 3

Conclusion

Now you know how to alert users. Use this knowledge for good, remember: “a great power comes with a great responsibility”, and don’t annoy all your users with useless and large text alerts.

Cheers!.

Tags: , , , , , , , , , ,

Software Engineer and iOS developer, from Caracas, Venezuela, with a tremendous weakness for hamburgers.

3 Responses to “Working with alerts” Subscribe

  1. mark whiffen June 16, 2011 at 5:45 PM #

    Great day! This is my 1st visit to your blog! We’re an accumulation of volunteers and beginning a new initiative inside a community in the identical niche. Your blog provided us helpful details to operate on. You have done an excellent job!

    • OscarVGG July 12, 2011 at 12:20 PM #

      Is always good to hear that you’re doing a good job. Thanks! :)

Trackbacks/Pingbacks

  1. The Programmer Lifestyle - November 3, 2010

    [...] To learn more about UIAlertView you can visit http://www.oscarvgg.com/working-with-alerts/ [...]

Leave a Reply

 

Marketie – Build your brand on twitter


The last few weeks a friend and I have been working on an app to help people market their brand [...]

First Look: Foundation Framework


Foundation is a framework for prototyping flexible web pages quickly and for any devices. It was created by ZURB, a U.S. [...]

Cocos2D Tips: Performance: CCSprite


Older iDevices (previous to iPhone 3GS) used to store images in power-of-two sizes of length and width. This means that [...]

Guess who’s back?


People is reading less, that’s a fact!. So blogs with very long posts are almost obsolete, at least in the [...]

How to make an iPhone App – Part 5: The Accelerometer


Yes, your iPhone has an accelerometer. I know this is not a physics class but the best way to define it is saying that it is a device to detect the magnitude and direction of acceleration. iDevices uses it to detect rotation, shake gestures, among others.

How to make an iPhone App – Part 4: Navigation Controller


Now that we know how to create a table views, we are going to use that knowledge to create a new project using a navigation controller to see a more detailed view of the tittle in the cell of the table view.