Reputation: 11193
i'm trying to make this work using 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. 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")
}
}
Upvotes: 0
Views: 221
Reputation: 94723
Your first problem is that {cancel in return }
is defining a closure with the input variable of cancel
and returning and doing nothing. 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
.
Second, you need to define your closures BEFORE the actual creation of your stringPicker so that you can pass them to the initializers:
@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()
}
Upvotes: 0
Reputation: 14010
I feel like there is something wrong with your usage of the variables done
and cancel
. 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()
}
Upvotes: 3