Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About UIAlertView with Textfield...

I have this code to prompt the UIAlertView, with UITextfield:

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")                                                       message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; [myTextField setBackgroundColor:[UIColor whiteColor]]; [myAlertView addSubview:myTextField]; [myAlertView show]; [myAlertView release]; 

But I would like to add a get the textfield value, after the user click "OK", and after the user click , I want to call a method, how can I assign that to the myAlertView? Thank you.

like image 530
DNB5brims Avatar asked Sep 03 '10 08:09

DNB5brims


People also ask

What is TextField in iOS?

A control that displays an editable text interface. iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+

What is TextField in Swift?

A TextField is a type of control that shows an editable text interface. In SwiftUI, a TextField typically requires a placeholder text which acts similar to a hint, and a State variable that will accept the input from the user (which is usually a Text value).


2 Answers

If you want to add a TextField to an UIAlertView, you can use this property (alertViewStyle) for UIAlertView:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"                                                 message:@"Message"                                                delegate:self                                       cancelButtonTitle:@"Done"                                       otherButtonTitles:nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; [alert release]; 

and in .h file of it add UIAlertViewDelegate as a protocol and implement the alertView:clickedButtonAtIndex delegate method in the .m file:

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

I hope, it works for you!

Note: "Available in iOS 5.0 and later"

like image 122
Hamed Rajabi Avatar answered Sep 20 '22 03:09

Hamed Rajabi


Declare the text field as global.And in the method of alertView clicked - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex just take the value of the textfield and do the operations you want with it.....

Heres the revised code

UITextField *myTextField; ... {  UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"New List Item", @"new_list_dialog")                                                       message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; [myTextField setBackgroundColor:[UIColor whiteColor]]; [myAlertView addSubview:myTextField]; [myAlertView show]; [myAlertView release]; } .... - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"string entered=%@",myTextField.text); } 

For iOS 5 and later You can use alertViewStyle property of UIAlertView.

Please refer Hamed's Answer for the same

like image 30
Suresh Varma Avatar answered Sep 22 '22 03:09

Suresh Varma