2

I am attempting to post data to a database, but the issue is that I cannot seem to obtain text that was entered in the UIAlertView from the user. I have tried many solutions such as subviewing a UITextField, but without luck. Here is my current, simple configuration. Let me know if you need more information.

- (void)viewDidLoad { [super viewDidLoad]; //load site in webview NSURLRequest *siteRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://google.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; [webview loadRequest:siteRequest]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Please enter your email address." delegate:self cancelButtonTitle:@"Submit" otherButtonTitles:@"Skip", nil]; alertView.alertViewStyle = UIAlertViewStylePlainTextInput; UITextField *emailInput = [alertView textFieldAtIndex:0]; [alertView show]; emails=emailInput.text; // NSLog(emails); phone=@"8675309"; [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } 
2

4 Answers 4

5

You need to get the email on an UIAlertViewDelegate method:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { UITextField *emailInput = [alertView textFieldAtIndex:0]; NSLog(@"%@",emailInput.text); } 
Sign up to request clarification or add additional context in comments.

Comments

2

You are trying to get the value before user enters it and even before alert view is shown to user.

UITextField *emailInput = [alertView textFieldAtIndex:0]; 

Use the delegate method and then try to get the value like this.

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { UITextField *emailInput = [alertView textFieldAtIndex:0]; } 

You have also missed to include this line

alertView.delegate = self; 

and don't forget to confirm to the protocol UIAlertViewDelegate in .h file like this

@interface yourViewController : UIViewController<UIAlertViewDelegate> 

Otherwise it won't call delegate method.

Hope this will help you.

Comments

0

You have allocated the memory for UIAlertview. So in the textfield you dont have any text initiallly . So you are not getting any text. Use the UIAlertVIew delegate method. In that access the textfield text.

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

Comments

0

Please use the UIAlertviewDelegate and its delegate method

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { UITextField *emailInput = [alertView textFieldAtIndex:0]; NSLog(@"Email Text is %@",emailInput.text); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.