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”.

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.

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.

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!.




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!
Is always good to hear that you’re doing a good job. Thanks!