iOS Beginner Series: UIAlertViews
Aug 26
This tutorial marks the launch of the beginner iOS tutorial series. UIAlertViews are the blue boxes that pop up in the middle of the screen. They look like the image on the left hand side.
Standard UIAlertViews have 3 components that are editable by the developer – there are some advanced things people have done, such as add sliders etc, but it’s not standard interface procedure and it doesn’t look that good. The components are the title, which is shown in bold at the top of the alert, the message, which is the text in the middle – The alert view expands in height if the message is too long, and the buttons. In this first part of the series i’ll explain how to launch one of these UIAlertViews with 1 button which dismisses the alert.
Firstly, set up a new “View-based Application” project in Xcode. Set the product name to whatever you want – i’ll be setting mine to “UIAlertView App”. Leave everything else as default.
Once the project has loaded, select “{Your App Name}ViewController.m” You should see a bunch of code in the right hand side. Find this function:
-(void)viewDidLoad
It will be commented out, and show green. To un-comment it, remove these two markings:
/* ... */
They’ll wrap round the viewDidLoad function. Once you’ve done that, add the following code before this line:
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test" message:@"This is an alert view" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
This initiates a new UIAlertView with title “Test”, message “This is an alert view” and button “Dismiss”. Once you’ve identified those in the above code, you can change them to whatever you want. This isn’t the end of the code we need though. We need to tell the application to show the alert. To do this, add this piece of code directly underneath what you just wrote.
[alert show];
Again, that’s not all! There’s one more piece of code we need. It’s a bit of memory management, and not strictly necessary, but it’s good practice. Add this below the [alert show]; instruction:
[alert release];
And that should now work. You can now run the application, and pat yourself on the back.
Part two will be about how to use multiple buttons, and how to handle when they are pressed.
RSS
Twitter