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.
}
user2793987
  • 199
  • 1
  • 2
  • 15
  • Refer the following links, http://stackoverflow.com/questions/4560346/getting-text-from-uialertview http://stackoverflow.com/questions/376104/uitextfield-in-uialertview-on-iphone-how-to-make-it-responsive http://stackoverflow.com/questions/2440053/uialertview-retreive-textfield-value-from-textfield-added-via-code http://stackoverflow.com/questions/4081277/how-do-i-get-text-from-uitextfield-in-an-uialertview – Venk Sep 19 '13 at 05:10
  • At the point you are reading, you will either get empty string or nil. The UIAlertView needs to be shown and text needs to be entered and then some button on UIAlertView needs to be pressed to say that the text has been entered. That's when you should read the textfield value. You are reading it even before the UIAlertView has been shown. Where will the textfield get its text from before anybody actually entering it. – SayeedHussain Sep 19 '13 at 05:11

4 Answers4

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);
}
Marcelo
  • 9,916
  • 3
  • 43
  • 52
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.

Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29
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);
}
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
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);

}
SayeedHussain
  • 1,696
  • 16
  • 23
Varun
  • 61
  • 4