In this tutorial will learn how to show Alert Box when text field is empty, The application we are going to do is based on View Based Application.
In this tutorial will learn how to show Alert Box when text field is empty, The application we are going to do is based on View Based Application.iPhone AlertView
In this tutorial will learn how to show Alert Box in iPhone when text field is empty. The application we are going to develop is based on View Based Application.
Final View will look like this:
My project name is AlertView, 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 and once Alert View is showed you wont be able to do anything on the view.
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.
AlertViewViewController.h file will look like this:
#import <UIKit/UIKit.h> @interface AlerViewViewController : 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 in AlertView.
AlertViewViewController.m file will look like this:
#import "AlerViewViewController.h" @implementation AlerViewViewController - (IBAction)showAlertBox:(id)sender { if([textField.text length]==0) { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert View" message:@"Enter text" delegate:self cancelButtonTitle:nil otherButtonTitles: nil]; [alert show]; UIAlertView *theAlert = [UIAlertView alloc] ; [theAlert show]; } [textField resignFirstResponder];// used to hide keyboard } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { // Release any retained subviews of the main view. } - (void)dealloc { [super dealloc]; } @end |
Make connection with Interface Builder as shown :
Finally Press Build And Go Button