我需要从alertcontroller中的文本字段中获取文本作为NSString,但是当我尝试返回某些内容时,我会收到错误"不兼容的块指针类型发送NSString"
我可以获取值并使其在处理程序中的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;
}];
完整的代码就在这里
- (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];
}
答案 0 :(得分:0)
此错误显示,因为在UIAlertAction处理程序中返回字符串。 您可以将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;
}];