2017-07-22 81 views
1

我已经得到了从试图找到一个答案,这也太沮丧,所以我会为一个问题...从UIAlertView中文本框中输入提取的Objective-C

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

这里是我的代码:

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

它显示了一个自定义按钮,一旦点击,执行“点击”的方法,其中执行ping主机。

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

感谢

+1

仅供参考 - 'UIAlertView'被弃用早在iOS版8.您应该使用'UIAlertController'。 – rmaddy

回答

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

要在创建得到中输入文字UIAlertView中文本字段

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]); 
} 
+0

我会在哪里放置这个? 另外,是否可以提醒输入的文本,而不是'NSLog'呢? –

+0

您想在按下ok后显示警报吗? – vp2698