ios - block 不被调用

标签 ios iphone swift objective-c-blocks

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

@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")
    }



}

最佳答案

我觉得您对变量 donecancel 的使用有问题。看起来您只是在匿名 block 中引用它们。你试过这个吗?

@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()
}

关于ios - block 不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26914040/

相关文章:

Swift - 字符串/字符到 ASCII,Int 值

ios - dyld : Library not loaded: @rpath/Alamofire. 框架/Alamofire

objective-c - UITableView 的 NSMutableArrays 或 NSArrays 的动态嵌套

ios - Swift segue fatal error

ios - 沙盒苹果支付测试握手失败

swift - 弹出模式中的中心堆栈 View

ios - 无法在 View Controller 中看到标签

ios - ScrollView 设置动画持续时间

iphone - 调整应用内屏幕截图的大小

swift - 如何解决 Swift 编译器错误 "error: Bus error: 10"?