Jacob Collins
Jacob Collins

Reputation: 115

Extracting input from a UIAlertView text box Objective-C

I have gotten too frustrated from trying to find an answer to this, so I will as a question...

How can I get the raw text from a UIAlertView text box in Objective-C?

Here is my code:

UIButton *AppCrashButton = [UIButton buttonWithType: UIButtonTypeCustom];
AppCrashButton.frame = CGRectMake(0, (self.view.frame.size.height - 44) / 6 * 1, self.view.frame.size.width, (self.view.frame.size.height - 44) / 6);

[AppCrashButton setTitle: @"Ping" forState: UIControlStateNormal];
[AppCrashButton addTarget: self action: @selector(Click) forControlEvents: UIControlEventTouchUpInside];

AppCrashButton.backgroundColor = [UIColor colorWithRed:0.19 green:0.19 blue:0.19 alpha:1.0];
AppCrashButton.layer.borderColor = [UIColor colorWithRed:0.96 green:0.26 blue:0.21 alpha:1.0].CGColor;
AppCrashButton.layer.borderWidth = 0.5f;

[self.view addSubview: AppCrashButton];
-(void) Click {
    UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: @"IP Ping" message: @"Please enter the IP address" delegate: self cancelButtonTitle: @"Cancel" otherButtonTitles: @"OK", nil];
    AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput;

    [AppCrashAlert show];
    [AppCrashAlert release];
}

It shows a custom button, that once clicked, executes the "Click" method, which pings a host.

I was wondering how I could get the text from the text input.

Thanks

Upvotes: 1

Views: 68

Answers (2)

Jitendra Tanwar
Jitendra Tanwar

Reputation: 249

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

To get entered text in UIAlertView text field

Upvotes: 1

vp2698
vp2698

Reputation: 1805

Set UIAlertViewDelegate delegate to Your controller

@interface YourViewController ()<UIAlertViewDelegate>

After that when you create your UIAlertView set it's delegate to Self In your case here

UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: @"IP Ping" message: @"Please enter the IP address" delegate: self cancelButtonTitle: @"Cancel" otherButtonTitles: @"OK", nil];
AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
// Set delegate 
AppCrashAlert.delegate = Self;
[AppCrashAlert show];
[AppCrashAlert release];

Then,

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

Upvotes: 1

Related Questions