iPhone UIAlertView 


 

iPhone UIAlertView 

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.

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 {

0

    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload {

1

}

- (void)dealloc {

    [super dealloc];

2

}

@end

Make connection with Interface Builder as shown :

3

Finally Press Build And Go Button

Download Here

4

Ads