user2793987
user2793987

Reputation: 199

Simple iOS Collect String from UIAlertView

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.
}

Upvotes: 2

Views: 1109

Answers (4)

Marcelo
Marcelo

Reputation: 9944

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);
}

Upvotes: 5

Prashant Nikam
Prashant Nikam

Reputation: 2251

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.

Upvotes: 2

Varun
Varun

Reputation: 61

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);

}

Upvotes: 0

Tendulkar
Tendulkar

Reputation: 5540

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);
}

Upvotes: 0

Related Questions