[英]Error when getting string from AlertController textfield
I need to get the text from a textfield in an alertcontroller as an NSString, but when i try to return something i get the error "Incompatible block pointer types sending NSString" 我需要从Alertcontroller中的文本字段中以NSString形式获取文本,但是当我尝试返回某些内容时,出现错误“发送NSString的不兼容块指针类型”
I can get the value and make it show in the NSLog within the handler, but not outside of it. 我可以获取该值并将其显示在处理程序中的NSLog中,但不能显示在其外部。
UIAlertAction *enterAction = [UIAlertAction
actionWithTitle:@"Enter"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"Enter Action");
NSString *name = ((UITextField *)[alertController.textFields objectAtIndex:0]).text;
NSString *time = ((UITextField *)[alertController.textFields objectAtIndex:1]).text;
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@", name);
NSLog(@"%@", time);
return name;
}];
The full piece of code is here 完整的代码在这里
- (void)textPopUp{
UIAlertController * alertController = [UIAlertController
alertControllerWithTitle:@"Attention!"
message:@"Please enter project name, and time spent below"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action){
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Cancel Action");
}];
UIAlertAction *enterAction = [UIAlertAction
actionWithTitle:@"Enter"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"Enter Action");
NSString *name = ((UITextField *)[alertController.textFields objectAtIndex:0]).text;
NSString *time = ((UITextField *)[alertController.textFields objectAtIndex:1]).text;
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@", name);
NSLog(@"%@", time);
return name;
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = NSLocalizedString(@"Name", @"Name");
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = NSLocalizedString(@"Time", @"Time");
}];
[alertController addAction:cancelAction];
[alertController addAction:enterAction];
[self presentViewController:alertController animated:YES completion:nil];
}
This error shows because in UIAlertAction handler you return string. 出现此错误的原因是,在UIAlertAction处理程序中您返回了字符串。 You can set textField text to controller like that: 您可以像这样将textField文本设置为控制器:
__weak TableViewController *weakSelf = self;
UIAlertAction *enterAction = [UIAlertAction
actionWithTitle:@"Enter"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSString *name = ((UITextField *)[alertController.textFields objectAtIndex:0]).text;
NSString *time = ((UITextField *)[alertController.textFields objectAtIndex:1]).text;
[alertController dismissViewControllerAnimated:YES completion:nil];
weakSelf.result = name;
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.