2016-07-03 3 views
1

にスウィフトブロックの変換、私は(私が思う)クロージャーメソッドを作成しました:スウィフトでのObjective-Cブロック

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]]; 
+0

あなたは何をしたいですか? 'UIButton'を押すと' UIAlert'を表示しますか?あなたがしたいことが何であるかを明確にしてください。 – Dershowitz123

+0

はい、それは私がやるべきことです。 – Lagos341

+0

なぜそれをしたいですか?ブリッジヘッダーを追加するだけで、SwiftとObjective-Cを同じプロジェクトで使用できます! – Sweeper

答えて

0

、Objective-Cのラフ同等。私はあなたが達成しようとしていることをはっきりとは分かっていませんが、次のコードはブロックfirstMoveと、メソッドaddActionから渡され呼び出される方法を定義しています。

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

[self addAction:firstMove]; 

...

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

聖なるたわごと。ありがとうございました! – Lagos341

-1

あなたは例を見ることができます。それは正しく動作するはずです。

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]; 
+0

'UIAlertView'は推奨されていません。ブロックで 'UIAlertController'を使用できるときに、なぜ、' UIAlertView'をデリゲートで使うべきですか? – Sulthan

+0

同じ意見ですが、私は答えを更新します。 –

関連する問題