2016-07-03 13 views
1

In Swift, ich eine Schließung Methode erstellt haben (glaube ich):Konvertierung Swift Block zu Objective-C-Block

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

Dass ich in diesem UIAlertAction Methode übergeben:

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

Wie kann ich Konvertieren Sie sowohl den Abschluss als auch die Methode in Objective-C?

ich tun haben versucht:

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

Und es wie folgt übergeben:

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

Was möchten Sie tun? Zeigen Sie ein 'UIAlert', wenn Sie ein' UIButton' drücken? Machen Sie sich klar, was genau Sie tun möchten. – Dershowitz123

+0

Ja, das möchte ich tun. – Lagos341

+0

Warum möchten Sie das tun? Fügen Sie einfach einen Bridging-Header hinzu und Sie können Swift und Objective-C im selben Projekt verwenden! – Sweeper

Antwort

0

Sie können für die Blöcke suchen, das Ziel-C grobe Entsprechung von Verschlüssen. Ich bin nicht ganz sicher, was Sie erreichen möchten, aber der folgende Code definiert Block FirstMove und wie es übergeben und von Methode AddAction aufgerufen wird.

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

[self addAction:firstMove]; 

...

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

Heilige Scheiße. Vielen Dank! – Lagos341

-1

können Sie das Beispiel sehen. Es sollte richtig funktionieren.

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' ist veraltet. Warum sollten wir 'UIAlertView' mit Delegaten verwenden, wenn wir' UIAlertController' mit Blöcken verwenden können? – Sulthan

+0

Ok, die gleiche Idee, ich werde die Antwort aktualisieren. –