如何在处理程序中指定 return 类型

How to specify return type in handler

-(bool)textFieldShouldClear:(UITextField *)textField {

    UIAlertController * blert = [UIAlertController alertControllerWithTitle:@"your alert" message:@"are you sure you want to clear" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction * defautact = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];

    UIAlertAction * defautact1 = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];

    [blert addAction:defautact];
    [blert addAction: defautact1];
    [self presentViewController:blert animated:YES completion:nil];
}

我正在尝试在其中使用 -(bool)textFieldShouldClear:(UITextField *)textField 我正在创建一个警报并包含两个按钮 okcancel 所以如果我按下按钮 ok 那么 -(bool)textFieldShouldClear:(UITextField *)textField 的 return 值必须是 YES 所以如果我按下按钮 cancel 那么 -(bool)textFieldShouldClear:(UITextField *)textField 的 return 值必须是 NO

你能告诉我如何在处理程序中执行此操作吗

您可以使用每个 UIAlertControl 的处理程序将全局 NSString 设置为是或否,然后您可以在 "self presentViewController" 之后对其进行评估。处理程序是一个指令块。

        UIAlertAction *YesAction = [UIAlertAction
                                       actionWithTitle:@"Yes"
                                       style:UIAlertActionStyleDefault
                                       handler:^(UIAlertAction *action)
                                       {
                                           NSLog(@"Tap on Yes");

                                           //Here you can set a global variable (reply for example) or make any action
                                           reply = @"yes";
                                       }];