【发布时间】: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