Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In iOS 5 there is a new and <em>easy</em> way to this. I'm not sure if the implementation is fully complete yet as it's not a gracious as, say, a <code>UITableViewCell</code>, but it should definitly do the trick as it is now standard supported in the iOS API. You will not need a private API for this.</p> <pre><code>UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"This is an example alert!" delegate:self cancelButtonTitle:@"Hide" otherButtonTitles:nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; [alert release]; </code></pre> <p>This renders an alertView like this (screenshot taken from the iPhone 5.0 simulator in XCode 4.2):</p> <p><img src="https://i.stack.imgur.com/35Fuj.png" alt="example alert with alertViewStyle set to UIAlertViewStylePlainTextInput"></p> <p>When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:</p> <pre><code>- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]); } </code></pre> <p>Here I just NSLog the results that were entered. In production code, you should probably keep a pointer to your alertView as a global variable or use the alertView tag to check if the delegate function was called by the appropriate <code>UIAlertView</code> but for this example this should be okay.</p> <p>You should check out the <a href="http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html" rel="noreferrer" title="Check under the alertViewStyle property!">UIAlertView API</a> and you'll see there are some more styles defined.</p> <p>Hope this helped!</p> <p>-- EDIT -- </p> <p>I was playing around with the alertView a little and I suppose it needs no announcement that it's perfectly possible to edit the textField as desired: you can create a reference to the <code>UITextField</code> and edit it as normal (programmatically). Doing this I constructed an alertView as you specified in your original question. Better late than never, right :-)?</p> <pre><code>UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; UITextField * alertTextField = [alert textFieldAtIndex:0]; alertTextField.keyboardType = UIKeyboardTypeNumberPad; alertTextField.placeholder = @"Enter your name"; [alert show]; [alert release]; </code></pre> <p>This produces this alert:</p> <p><img src="https://i.stack.imgur.com/of77H.png" alt="UIAlertView that uses the UIAlertViewPlainTextInput alertStyle to ask a user name"></p> <p>You can use the same delegate method as I poster earlier to process the result from the input. I'm not sure if you can prevent the <code>UIAlertView</code> from dismissing though (there is no <code>shouldDismiss</code> delegate function AFAIK) so I suppose if the user input is invalid, you have to put up a new alert (or just re<code>show</code> this one) until correct input was entered.</p> <p>Have fun!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload