Subscribe ( RSS )

iPhone Programming Tutorials


 

iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Screen shot 2009-11-09 at 8.12.11 AM copyThis will be a simple tutorial showing you how to put a UITextField in a UIAlertView. This is simple and just a couple lines if code. You will learn CGAffineTransform and coding UITextField programmatically.

Heres a screenshots of what we should get.

Screen shot 2009-11-09 at 8.12.11 AM

So lets go ahead and get started…

1. Create A New View Based Application
You can name it whatever you want, I am gonna name it TextFieldInAlert.

2. Implementing The Code
Jump in the viewcontroller.m or if you called it TextFieldInAlert then TextFieldInAlert.m Now find the -(void)viewDidLoad method. Uncomment it and put this code in there.

- (void)viewDidLoad {

[super viewDidLoad];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Enter Name Here” message:@”this gets covered!”

delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil];

UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];

[myTextField setBackgroundColor:[UIColor whiteColor]];

[alert addSubview:myTextField];

[alert show];

[alert release];

[myTextField release];

}

So we are basically calling a UIAlertView and then we are adding the UITextField programmatically. You might have noticed in the message part of the UIAlertView we put “this gets covered!”, if we didn’t put that sentence then the alerts buttons would go up more and the UITextField will be messed. You can try taking that line out and see what happens. Now Build and Run the app. Now you got the UITextField to be inside the UIAlertView. Now try tapping the UITextField. Uh oh, why is the Keyboard covering the UIAlertView? Well there is just a simple fix to this. We just add two more lines of code and it will fix that. Add this to your code

CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);

[alert setTransform:myTransform];

So now your full code should be looking like this.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Enter Name Here” message:@”this gets covered!” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil];

UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];

CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);

[alert setTransform:myTransform];

[myTextField setBackgroundColor:[UIColor whiteColor]];

[alert addSubview:myTextField];

[alert show];

[alert release];

[myTextField release];

Now if you Build and Run, you will notice the UITextField is a little higher and when you tap the UITextField the Keyboard doesn’t cover it up anymore. That is what the CGAffineTransform was for. So that is basically it! There is a video tutorial also available and if you like video tutorial more then written ones you can check it out out by clicking HERE. You can download the source code below. Happy iCoding!

iPhone Tutorial – UITextField In A UIAlertView

 

16 Responses

DevinsiPodHelp Says:

November 9th, 2009 at 8:48 pm

Great post dude.. I really expect more tutorials like this!

mOuse Says:

November 10th, 2009 at 2:23 am

Hey, a really useful post!

Do you think this is in-keeping with Apple’s UI guidelines? I’ve done this for a while in an App, but I recently decided to switch to a modal view because I wasn’t convinced I was doing the right thing. The other reason I switched was that it takes ages for the UI to appear on a non-3gs phone.

Constantine Says:

November 10th, 2009 at 3:10 am

How Can I post my topic in english on this blog?

john Says:

November 10th, 2009 at 4:13 am

Hi there,

This is great. Thanks you.

Could you post a little code to show how to access the value of the text box, say, in a

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

method.

Thanks.

Shane Says:

November 10th, 2009 at 6:12 am

Great stuff. Thanks.

AppStoreMod Says:

November 10th, 2009 at 9:53 am

No. There is a lot of people that used this in there applications and it was fine with Apple and also it didn’t slow down there app at all. It might be something your doing in your code.

mOuse Says:

November 10th, 2009 at 1:33 pm

Yeah maybe… I think it was because I was forcing the keyboard to appear at the same time.

spoonforknife Says:

November 10th, 2009 at 5:13 pm

Rather than adding an extraneous message line, wouldn’t it be better (as long as you’re customizing your UIAlertView) to simply make a custom UIAlertView and override the default setFrame method? This would then allow you to manage the UIAlertView far more easily.

spoonforknife Says:

November 10th, 2009 at 8:31 pm

You also have a memory leak from not releasing the UITextField.

hugo Says:

November 11th, 2009 at 6:26 pm

mayby with animation?

- (void)viewDidLoad {
[super viewDidLoad];

alert = [[UIAlertView alloc] initWithTitle:@”Enter Name Here” message:@”this gets covered!” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles:@”OK!”, nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
myTextField.delegate = self;

[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, -60);
[alert setTransform:myTransform];

[UIView commitAnimations];
}

Chris Says:

November 13th, 2009 at 10:33 am

Can I assume that this method would work with two UITextFields, like if I was implementing a UserName & Password AlertView?

Matt Says:

November 18th, 2009 at 4:52 pm

Great post – thanks.

Just one question how do you get the value from the text field?

I have tried adding textFieldDidEndEditing, but i never get in the method.

I do have ever jump to my -(void)allertView: etc method but can’t then get to my text box value.

Your help would be much appreciated!

Thanks!

Matt Says:

November 18th, 2009 at 4:59 pm

Don’t worry I sorted it… just declared the UITextField *myTextField; in the header file and not the alert method code.

Then used myTextField.text in the alertView method…

Easy, just needed to use my brain!

Des Says:

November 21st, 2009 at 11:12 am

Had a little trouble at first but finally sorted it – many thanks

iPhoneGeek 爱疯极客 » 在UIAlertView中加入UITextField Says:

November 21st, 2009 at 10:06 pm

[...] 原文见:iPhone Coding Tutorial – Inserting A UITextField In A UIAlertView   用户界面, 编程   用户界面, 编程, UIAlertView, 教程   [...]

Tom Says:

November 22nd, 2009 at 4:44 am

Great tutorial nice and simple and right to the point.

I ran into an issue with this though. If you add a button on the view that brings up the alert with text field you can hold down inside the text field and let go and the ‘paste’ option appears as usual.

But after the first time if you bring up the alert a second time (and every time thereafter) the paste option no longer works! The little magnifying glass still pops up but when it goes away, there is no paste option!

Anyone have any idea why this might be?

Leave a Reply