UIAlertView contains UITextField

Target: 

The updated UIAlertView now has a style which allows a Text input field in UIAlertView i.e.

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

This works well but I wanted to init the input text with a defualt text like “sample”.

I see that folks in the past have used undocumented api like (which works great)

[alert addTextFieldWithValue:@"sample text" label:@"Text Field"];

but since this is still not an official Apple public API I can’t use it.

Any other way to handle this? I did try to init in willPresentAlertView but text field seem to be read-only.

 

Answer:

The UIALertView has a textFieldAtIndex: method that returns the UITextField object you want.

For a UIAlertViewStylePlainTextInput, the index of the textfield is 0.

You can then set the placeholder (or text) property of the textfield:

UIAlertView *alert = ....
UITextField *textField = [alert textFieldAtIndex:0];
textField.placeholder = @"your text";

UIAlertView Class Reference

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s