Alert View Application in iPhone

In this application we will see how to AlertView display after button pressed in iPhone. So let see how it will

Step 1: Open the Xcode, Create a new project using View Base application. Give the application

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib, AlertViewViewController.xib for the AlertView application.

Step 4: Open the AlertViewViewController.h file and make the following changes:


#import <uikit/UIKit.h>
@interface AlertViewViewController : UIViewController {
IBOutlet UITextField *text;
- (IBAction)AlertView:(id)sender;



Step 5: Double click the AlertViewViewController.xib file and open it to the interface Builder. Drag the Textfield, Label and round rect button from the library and place it to the View window. Now connect File’s Owner icon to the TextField and select text, select the button and bring up Connection Inspector and connect Touch Up Inside to the File’s Owner icon and select AlertView: method. Now Save the .xib file, close it and go back to the Xcode.

Step 6: Open the AlertViewViewController.m file and make the following changes:


#import "AlertViewViewController.h"
@implementation AlertViewViewController
- (IBAction)AlertView:(id)sender
if([text.text length]==0)
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@""
message:@"Enter Your Name"
otherButtonTitles: nil];
[alert show];
text resignFirstResponder];
- (void)dealloc
[super dealloc];
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
[super viewDidLoad];
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);



Step 7: Now Compile and run the application on the Simulator.

You can Download SourceCode from AlertView

Leave a Comment: