Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to popup a text field

Tags:

iphone

sdk

ios4

I have a view in my app with many labels and a toolbar with some buttons. I would like to edit all the labels at the same time when pressing a button. I thought it might be a way to "popup" a text input and make a keyboard appear at the same time. Which is the cleanest way to do that? Thank you

like image 511
k20 Avatar asked Dec 02 '25 08:12

k20


2 Answers

http://discussions.apple.com/thread.jspa?messageID=8445879

Alternatively. Here's how I do it:

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Please enter your name:"
                                                 message:@"\n\n"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"Enter", nil];

textField = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setPlaceholder:@"Name for message"];
[prompt addSubview:textField];
[textField release];

// show the dialog box
[prompt show];
[prompt release];

// set cursor and show keyboard
[textField becomeFirstResponder];

Then in the UIAlertView delegate method you read the content from textField.text :-)

like image 140
Nevin Avatar answered Dec 03 '25 23:12

Nevin


I know its too late answer,But this answer for those who are using iOS 5.0 or later. Now AlertView has new Property AlertViewStyle for various purpose.

UIAlertViewStyle
The presentation style of the alert.

typedef enum {
   UIAlertViewStyleDefault = 0,
   UIAlertViewStyleSecureTextInput,
   UIAlertViewStylePlainTextInput,
   UIAlertViewStyleLoginAndPasswordInput
} UIAlertViewStyle;

Example:

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Title"  message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
         [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
         [alertView show];
         [alertView release];

Delegate:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"%@",[[alertView textFieldAtIndex:0] text])
}

Reference : http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html

like image 26
iNeal Avatar answered Dec 04 '25 01:12

iNeal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!