Adding a UITextField inside an UIAlertView
Hi, i’m sure that you wondered how you can create an input inside an alert, those password alerts, or just to enter a single value like name or something like this.
The solution is very simple and efficient, everything you should know is that both the alert and the text field are actually views, so you can add an view inside an other view using the addSubview:view method… yes, it’s that simple, anyhow here is how you can do it.
I will guide you step by step to do this in a simple project, but you can integrate this as it is in any other projects, just follow the steps.
First, create a new view based project and add a method which will be called when you press a button, you .h file of the view controller should look something like this :
#import <UIKit/UIKit.h>
@interface alertViewController : UIViewController {
}
-(IBAction)buttonPressed:(id)sender;
@end
Now also add the method in the .m file, for now it’s just going to be empty, we will add the code later. Add this just before the @end liness :
-(IBAction)buttonPressed:(id)sender {
}
Now, go to interface builder, drag a button from the library and connect the event to the method :
You are now done with interface builder, you can close it and return to xCode.
Go to the method you just entered and enter the code :
UIAlertView *alert = [[[UIAlertView alloc]initWithTitle:@"Add Tag" message:@" " delegate:self cancelButtonTitle:@"Add" otherButtonTitles:@"Cancel",nil]autorelease]; UITextField *tagField = [[[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)] autorelease]; // tagField. tagField.backgroundColor = [UIColor whiteColor]; tagField.tag = 10; [alert show]; [alert addSubview:tagField];
The code is pretty straight forward, we create an alert view and a text field, set the frame for the text field add a white background to the text field so it can be seen, and also add a tag for the button so we can retrieve it at a later time, then the alert is shown and the text field is added.
Regarding the background of the uitextfield, by default it looks like it’s transparent…. so if you don’t add the color you will think that the field is not added, actually it’s there, you can click it but you can’t see it… it happened to me, so don’t do this mistake again.
In this example I used an alert view to add a tag, but you can use it for what ever you want, if you want a simple way to add a value.
Now, go back to the .h file and add the UIAlertViewDelegate
@interface alertViewController : UIViewController <UIAlertViewDelegate> {
We have to do this so we can track when the user clicks one of the buttons from the alert. You can also build and run now, but no matter what button you press the action will be the same.
To get the actions add this code :
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
//the add button
UITextField *tagName = (UITextField*)[alertView viewWithTag:10];
if(buttonIndex == 0) {
if(tagName.text != nil) {
// do some tag adding
}
}
[tagName removeFromSuperview];
}
You can see that we are using the tag we set earlier to get the text view.
Also this method is called before the alert is dismissed so theĀ text view is removed avoiding warnings that appear when the alert view is dismissed.
And this is it, you can build and run and see the results :
Please do not hesitate to ask me any questions if you have any!



August 19th, 2010 at 5:28 am
Nice Blog,
BE CONTINUE:)