Home Tutorial Iphone Examples iPhone UIAlertView 

 
 

Share on Google+Share on Google+
iPhone UIAlertView 
Posted on: September 8, 2009 at 12:00 AM
Advertisement
In this tutorial will learn how to show Alert Box when text field is empty, Alert box will contain one button i.e. ok button. The application we are going to do is based on View Based Application.

UIAlertView 

In this tutorial will learn how to show Alert Box message when text field is empty, Alert box will contain one button i.e. ok button. The iPhone application we are going to do is based on View Based Application.

Final View will look like this:

My project name is UIAlertView, is view based Application. To show Alert view when text field is empty will add some codes in .h and .m file and will show Alert view only when the button is clicked so will write Alert View code into the Button Action, so whenever button is clicked will show you Alert box with OK button and when you press OK button will allow you to enter text into text field.
Now to enter text we need TextField and also Button to show Alert View and action to button, so will declare it in .h file and file is

UIAlertViewViewController.h file will look like this:

#import <UIKit/UIKit.h>

@interface UIAlertViewViewController : UIViewController

{

IBOutlet UITextField *textField;

}

- (IBAction)showAlertBox:(id)sender;

@end

In .m will write the method for that action and in that will write code for Alert View.In action method will check the length of textfield by using if condition and if textfield is equal to zero then at button click will show alert view and in that will set the title, message and the name for the button used in AlertView.

UIAlertViewViewController.m file will look like this:

#import "UIAlertViewViewController.h"

@implementation UIAlertViewViewController

- (IBAction)showAlertBox:(id)sender

{ if([textField.text length]==0)

    {

        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert View"

                                                       message:@"Enter text"

                                                        delegate:self

                                             cancelButtonTitle:@"OK"

                                             otherButtonTitles:nil];

      [alert show];

    }

    [textField resignFirstResponder]; // is to hide the keyboard when button is clicked

}  

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

}

- (void)dealloc {

    [super dealloc];

}

@end

Make connection with Interface Builder as shown :

Finally Press Build And Go Button

Download Here

Advertisement

Related Tags for iPhone UIAlertView :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 8, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com