阻止未被调用

时间:2014-11-13 16:48:10

标签: ios iphone swift objective-c-blocks

我正在尝试使用swift https://github.com/skywinder/ActionSheetPicker-3.0来完成这项工作。问题是当我点击完成按钮或取消按钮时,没有调用块。这是怎么回事?我已将doneBlock设置为done变量,并将cancelBlock设置为取消变量。日志中没有返回任何内容?

@IBAction func openClosePicker(sender: UIButton!) {
    var stringPicker = ActionSheetStringPicker(title: "Nav Bar From Picker", rows: ["One", "Two", "A lot"], initialSelection: 1, doneBlock: {done in return}, cancelBlock: {cancel in return }, origin: sender.superview!.superview)

    stringPicker.showActionSheetPicker()

    let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
        println(selectedValue)

    }

    let cancel: ActionStringCancelBlock = {(picker: ActionSheetStringPicker!) in
        println("Block Picker Canceled")
    }



}

2 个答案:

答案 0 :(得分:3)

我觉得您使用变量donecancel时出现了问题。看起来你只是在一个匿名块中引用它们。你试过这个吗?

@IBAction func openClosePicker(sender: UIButton!) {
  let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
    println(selectedValue)
  }

  let cancel: ActionStringCancelBlock = {(picker: ActionSheetStringPicker!) in
    println("Block Picker Canceled")
  }

  var stringPicker = ActionSheetStringPicker(title: "Nav Bar From Picker",
                                             rows: ["One", "Two", "A lot"],
                                             initialSelection: 1,
                                             doneBlock:done,
                                             cancelBlock:cancel,
                                             origin: sender.superview!.superview)

  stringPicker.showActionSheetPicker()
}

答案 1 :(得分:0)

你的第一个问题是{cancel in return }正在使用cancel的输入变量定义一个闭包,并且返回并且什么都不做。更长的版本是:

{ (cancel: ActionSheetStringPicker!) -> () in
    return
}

相反,现有的闭包只是通过其名称来引用。您应该直接引用您的闭包:canceldone

其次,您需要在实际创建stringPicker之前定义闭包,以便将它们传递给初始化器:

@IBAction func openClosePicker(sender: UIButton!) {
    let done = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
        println(selectedValue)

    }

    let cancel = {(picker: ActionSheetStringPicker!) in
        println("Block Picker Canceled")
    }

    var stringPicker = ActionSheetStringPicker(
        title: "Nav Bar From Picker",
        rows: ["One", "Two", "A lot"],
        initialSelection: 1,
        doneBlock: done,
        cancelBlock: cancel,
        origin: sender.superview!.superview
    )

    stringPicker.showActionSheetPicker()
}