【问题标题】:Converting Swift block to Objective-C block将 Swift 块转换为 Objective-C 块
【发布时间】:2016-07-03 04:16:18
【问题描述】:

在 Swift 中,我创建了一个闭包方法(我认为):

func firstMove(action: UIAlertAction!) {
        if action.title == "Yes" {
            currentPlayer = "X"
        } else {
            currentPlayer = "0"
        }

我传入这个 UIAlertAction 方法:

let optionToStartController = UIAlertController(title: "", message: "Do you want first move?", preferredStyle: .Alert)
            optionToStartController.addAction(UIAlertAction(title: "Yes", style: .Default, handler: firstMove))

如何将闭包和方法都转换为 Objective-C?

我已经尝试过:

- (void)firstMove:(UIAlertAction*)action
{
  if ([action.title isEqual: @"Yes"]) {
    _currentPlayer = 'X';
} else {
    _currentPlayer = 'O';
 }
}

并像这样传递它:

UIAlertController *optionToStartController = [UIAlertController alertControllerWithTitle:@"" message:@"Do you want first move?" preferredStyle:UIAlertControllerStyleAlert];
[optionToStartController addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler: firstMove]];

【问题讨论】:

  • 你想做什么?按UIButton 时显示UIAlert?明确你想要做什么。
  • 是的,这就是我想做的。
  • 你为什么要这样做?只需添加一个桥接头,您就可以在同一个项目中使用 Swift 和 Objective-C!

标签: objective-c xcode swift closures


【解决方案1】:

您可能正在寻找块,它在 Objective-C 中大致相当于闭包。我不太确定您要完成什么,但以下代码定义了块 firstMove 以及它是如何从方法 addAction 传递和调用的。

void(^firstMove)(int) = ^(int x){
    NSLog(@"print y: %d", x);
};

[self addAction:firstMove];

...

-(void)addAction:(void(^)(int))y{
    y(5);
}

【讨论】:

    【解决方案2】:

    您可以查看示例。它应该可以正常工作。

    UIAlertController * alert=   [UIAlertController
                                     alertControllerWithTitle:@"Question"
                                     message:@"Do you want first move?"
                                     preferredStyle:UIAlertControllerStyleAlert];
        //First button
       UIAlertAction* ok = [UIAlertAction
                            actionWithTitle:@"YES"
                            style:UIAlertActionStyleDefault
                            handler:^(UIAlertAction * action)
                            {
    //Actions if the user press this button
    //Dismiss the alertController. It's preferred to be in all actions.
                                [alert dismissViewControllerAnimated:YES completion:nil];
    _currentPlayer = @"X";
    
                            }];
    
    //Second Button
       UIAlertAction* cancel = [UIAlertAction
                                actionWithTitle:@"NO"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action)
                               {
    //Actions if the user press this button
    //Dismiss the alertController. It's preferred to be in all actions.
                                   [alert dismissViewControllerAnimated:YES completion:nil];
    _currentPlayer = @"O";
    
                               }];
        //Add the actions to the alertController
       [alert addAction:ok];
       [alert addAction:cancel];
    
    
    //present the alertController on the device. If you are writing this in the viewController, you can use self.view, if you are in UIView, then just self
       [self.view presentViewController:alert animated:YES completion:nil];
    

    【讨论】:

    • UIAlertView 已弃用。当我们可以将UIAlertController 与块一起使用时,为什么还要将UIAlertView 与代表一起使用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多