2016-01-06 58 views
2

此前已询问过此问题的位置:Get input value from TextField in iOS alert in Swift。但是,有人拥有Objective-C语言的代码吗?如何在警报中从TextField中获取输入文本

在此先感谢!

我到目前为止已经得到:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Adding A Row" 
    message:@"Enter A Number" 
    preferredStyle:UIAlertControllerStyleAlert]; 

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
    textField.placeholder = @""; 
}]; 

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    [alert dismissViewControllerAnimated:YES completion:nil]; 
}]; 

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
//Do some action here 
}]; 

[alert addAction:cancel]; 
[alert addAction:ok]; 

[self presentViewController:alert animated:YES completion:nil]; 
+0

这不应该太难翻译。到目前为止,Objective-C代码的哪一部分?你是否已经创建了一个'UIAlertController'?你有没有添加文本框? – luk2302

+0

我已经添加了一个文本框。遇到问题:// 3。从文本字段获取值,并在用户单击确定时打印。 (0)(0)(0)(0)收藏文章登录我的社区来说两句吧...表情删除后不可恢复,是否删除取消确定图片正在上传,请稍后...若前些页面显示空白,你可以按<确定>键。 textField.text)“) })) – Jay

+0

好的,请显示您已获得的代码以及您认为需要帮助的地方,如果显示您的代码,将很乐意提供一些代码。 – luk2302

回答

4

您需要使用相同的逻辑,在Swift:通过以下方式写alert.textFields[0].text动作处理程序中:

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
    NSString *input = alert.textFields[0].text; 
    NSLog(@"input was '%@'", input); 
}]; 

在哪我的测试版

输入为'1234'

+0

它的工作!谢谢! :d – Jay