简体   繁体   English

阻止不被调用

[英]Block not being called

i'm trying to make this work using swift https://github.com/skywinder/ActionSheetPicker-3.0 . 我正在尝试使用swift https://github.com/skywinder/ActionSheetPicker-3.0进行这项工作。 The problem is when i click the done button or cancel button the blocks are not being called. 问题是,当我单击完成按钮或取消按钮时,不会调用这些块。 How come is this? 怎么会这样 i've set the doneBlock to be the done variable and the cancelBlock to be cancel variable. 我将doneBlock设置为done变量,将cancelBlock设置为cancel变量。 Nothing is returned in the log? 日志中什么都没有返回?

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



}

I feel like there is something wrong with your usage of the variables done and cancel . 我觉得您对donecancel变量的使用有问题。 It looks like you are simply referring to them within an anonymous block. 看起来您只是在匿名块中引用它们。 Have you tried this? 你有尝试过吗?

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

Your first problem is that {cancel in return } is defining a closure with the input variable of cancel and returning and doing nothing. 您的第一个问题是{cancel in return }正在定义一个闭包,其输入变量为cancel{cancel in return } ,什么也不做。 The longer version of this is: 这个的较长版本是:

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

Instead, an existing closure is simply referred to by its name. 取而代之的是,现有闭包仅用其名称来指代。 You should just reference your closures directly: cancel and done . 您应该直接引用闭包: canceldone

Second, you need to define your closures BEFORE the actual creation of your stringPicker so that you can pass them to the initializers: 其次,您需要在实际创建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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM