views:

13808

answers:

6

I placed a UITextField into a UIAlertView and moved it up so the keyboard wouldn't cover it up with the following code:

[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];
UITextField * nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];

I also have the following code to deal with the alert view:

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{ 
 NSLog(@"%d", (int) buttonIndex);
 if (buttonIndex == 1) { // OK pushed
  NSLog([alert textField].text); // does not log anything
 } else {
  // Cancel pushed
}}

I also have a UIAlertViewExtended.h file that contains:

@class UITextField, UILabel;

@interface UIAlertView(Extended)

-(UITextField *)textField;

@end

My question is how do I get the text the user entered and how do I dismiss the keyboard?

Thanks, Ben

+6  A: 

For those who may care, here's a working solution:

UIAlertView* dialog = [[UIAlertView alloc] init];
[dialog setDelegate:self];
[dialog setTitle:@"Enter Name"];
[dialog setMessage:@" "];
[dialog addButtonWithTitle:@"Cancel"];
[dialog addButtonWithTitle:@"OK"];

nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
[dialog addSubview:nameField];
CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 100.0);
[dialog setTransform: moveUp];
[dialog show];
[dialog release];
[nameField release];

Make sure you've created UITextField * nameField; in your .h file, then you can get at the text the user typed in by doing: inputText = [nameField text];

in the - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex method.

Ok, that works pretty well, but how do I get the buttons to move down to make room for the text field? On my implementation (which starts as a copy/paste of yours), I have 4 buttons (Cancel, foo, bar, baz), and the text field overlays the cancel button.
Olie
Hey! Why are you retaining the variable `dialog`? Isn't it already allocated by you? I might be wrong but it seems that you are leaking an object this way. The rule of thumb is that if you alloc an object then you must release it because it already has a retain count of 1.
inkredibl
@inkredibl for the sake of posterity, I removed the retain which led to a memory leak.
Epaga
+4  A: 

A simple way to locate the text field, without keeping an explicit reference to it, is to set its tag:

nameField.tag = ALERTVIEW_NAMEFIELD;

Make sure it is different from 0 and from other UIView object tags you may have, including the parent dialog!

Then inside the alertView:clickedButtonAtIndex: handler, you can retrieve your text field this way:

UITextField *nameField = (UITextField *)[alertView viewWithTag:ALERTVIEW_NAMEFIELD];
squelart
Neat, learn something new every day..
pix0r
+5  A: 

It's worth checking out

http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html?cmd=success#comment3870

For a complete and comprehensive solution.

Adam Ernst
The code at the URL is clean, easy to use.
freespace
Great code. I recommend saving the text field in an instance variable. Then, you don't need to implement the text field delegate and you can have it resign first responder in `alertView:willDismissWithButtonIndex:` to get rid of the "wait_fences: failed to receive reply: 10004003" console messages.
gerry3
A: 

At the same time i would like to ask, How to align text vertically Cenetered in UItextField..

I found for a textfield with height , whose text is 'boldSystemFontOfSize:15' is placed in textfield with a upward shift. I want to make the text centered (vertically) irrespective of the fotnSize...

Thanks, SujithKris

[textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
adam
Ask in a separate question.
Emil
+1  A: 

A quite nice approach is, you can use the delegate methods of the UITextFieldDelegate to move the dialog up only when the keyboard is activated. See below.

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self];

    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -20);
    [dialog setTransform: moveUp];

    [UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self];

    CGAffineTransform moveDown = CGAffineTransformMakeTranslation(0.0, 0.0);
    [dialog setTransform: moveDown];

    [textField resignFirstResponder];

    return YES;
}
adam
+1  A: 

Found more clear solution. Check this code snippet out: Username and Password UITextFields in UIAlertView prompt

slatvick