从UIAlertView文本框Objective-C中提取输入

时间:2017-07-22 10:13:40

标签: objective-c input uialertview

我因为试图找到答案而感到非常沮丧,所以我将作为一个问题......

如何从Objective-C中的UIAlertView文本框中获取原始文本?

这是我的代码:

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

它显示了一个自定义按钮,一旦点击,就会执行" Click" ping主机的方法。

我想知道如何从文本输入中获取文本。

由于

2 个答案:

答案 0 :(得分:1)

Name servers misconfigured for XXXXXXXX.de
Error : Nameserver ns1.XXXXXXXXXXX.com cannot be queried for SOA
Error : Nameserver ns2.XXXXXXXXXXX.com cannot be queried for SOA

UIAlertView 文本字段

中输入文字

答案 1 :(得分:1)

UIAlertViewDelegate委托设置为您的控制器

@interface YourViewController ()<UIAlertViewDelegate>

之后,当您创建UIAlertView时,将其委托给Self代表您的案例

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

然后,

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