【发布时间】:2016-02-13 18:10:05
【问题描述】:
我有一个 UIAlertController,它包含一个文本字段和 2 个按钮:确定和取消。我想检索用户在按下 OK 时在文本字段中输入的文本,但由于文本字段位于块中,我无法从 OK 按钮块访问块内的 textField.text 字段。我如何访问它并使用用户输入的文本?请参阅下面的代码。
- (IBAction)saveItinerary:(id)sender
{
NSString *itineraryNameInput = [[NSString alloc] init];
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Save Itinerary"
message:@"Enter Name for the Itinerary"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Name";
textField.text = itineraryNameInput;
}];
NSLog(@"name = %@", itineraryNameInput);
[self presentViewController:alert animated:YES completion:nil];
}
【问题讨论】:
-
您是否尝试设置实例变量(例如强属性)并将其设置为指向文本字段?
@property (strong, nonatomic) UITextField *myAlertTextField;在您的视图控制器中,然后self.myAlertTextField = textField;在块内?
标签: ios objective-c iphone xcode uialertcontroller