In this tutorial you are going to learn how to make an alert box appear after you press a button.
We have to take memory into account or else if we were going to press the button the application would quit.
First step
Create a new view based application called 'alert'
next go to alertViewController.m (the .m file stores the classes, a class is a snippet of code which can be called at any time. Instead of writing the code multiple times, you only have to write it once)
Write this code into the .m file:
//button pressed
- (IBAction)buttonPressed:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Your Messaage" delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
Notice the [alert release], this is used to give the memory back to the cpu.
Second Step
Next go to alertViewController.h, (the .h is like the main, this is the area where all the action goes down)
Inside the .h file copy the code below:
@interface alertViewController : UIViewController {
UIButton *button;
}
@property (nonatomic, retain) IBOutlet UIButton *button;
-(IBAction) buttonPressed:(id)sender;
@end
Notice the *button, this is how you name objects in objective C.
The UIButton is simply the button
Notice the @property (nonatomic, retain) part, this is again used for memory,
You may think but how could such a simple function need so much memory control.
Try the program without @property (nonatomic, retain) and see what happens.
Third step
Next go to your interface builder by double clicking alertViewController.xib
Drag a round rect button to the view, once done go to the connections panel and press the touch up inside, drag the line to the files owner and press buttonPressed.
Save and click build and run.
-- End of tutorial --
Have a look at the video for a visual step by step tutorial.