I need to add a TextField
to an UIAlertView
. I understand that apple discourage this approach. So is there any library that i could make use of to add
Try something like this:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title"
message:@"\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil] autorelease];
CGRect rect = {12, 60, 260, 25};
UITextField *dirField = [[[UITextField alloc] initWithFrame:rect] autorelease];
dirField.backgroundColor = [UIColor whiteColor];
[dirField becomeFirstResponder];
[alert addSubview:dirField];
[alert show];
Unfortunately the only official API for this is iOS 5 and up, it's a property called alertViewStyle
which can be set to the following parameters:
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput
UIAlertViewStylePlainTextInput
UIAlertViewStyleLoginAndPasswordInput
UIAlertViewStylePlainTextInput
being the one you want.
Messing with the view hierarchy as described above is strongly discouraged by Apple.
adding to answer from 'Shmidt', the code to capture text entered in UIAlertView is pasted below (thank you 'Wayne Hartman' Getting text from UIAlertView)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.userNumber = [alertView textFieldAtIndex:0].text;
if (self.userNumber) {
// user enetered value
NSLog(@"self.userNumber: %@",self.userNumber);
} else {
NSLog(@"null");
}
}
}
first of All Add UIAlertViewDelegate into ViewController.h File like
#import <UIKit/UIKit.h>
@interface UIViewController : UITableViewController<UIAlertViewDelegate>
@end
and than Add Below Code where you wants to alert Display,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Done"
otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
and it's delegate method which returns what input of UItextField
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%@", [alertView textFieldAtIndex:0].text);
}
As of iOS 8 UIAlertView
has been deprecated in favor of UIAlertController, which adds support for adding UITextField
s using the method:
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;
See this answer for an example.
refer this... http://iosdevelopertips.com/undocumented/alert-with-textfields.html this is private api and if you use it for app in appstore it might get rejected but it is fine for enterprise development.