How to create an Alert View

Here’s how we can create a simple pop-up window via the UIAlertView instance. It can have a title, some info text and several buttons (OK, Cancel, etc). We’ll only deal with one button and not worry about how to read out which button value has been pressed.

This is what a UIAlertView looks like

And here’s how we build this on Objective-C:

UIAlertView *infoMessage;
infoMessage = [[UIAlertView alloc]
               initWithTitle:@"Image Switcher 1.0" message:@"This app switches 
               between several UIIMageViews and displays a new picture in each one. 
               It also uses the segmented control thingy. 
               Check out <a href="http://www.versluis.com/" rel="nofollow">http://www.versluis.com</a> for more goodies.&quot; 
       delegate:self cancelButtonTitle:@&quot;Exciting stuff!&quot; otherButtonTitles:nil];
infoMessage.alertViewStyle = UIAlertViewStyleDefault;
[infoMessage show];

This is quite a long block of code so here are the steps:

  • create an instance of the UIAlertView
  • allocate and initiate it with parameters Title, Message, and an array of buttons (cancel and nil if you’re only using a single button to dismiss the view)
  • give the view a style (mandatory)
  • display the view

You can add a \n for a line break inside the window.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.