I'm trying to use an ActionSheetStringPicker in my iOS Swift app. I can't figure out how to use the done block or the success action. Here is my code:
func pickerDone()
{
println("Here I am!")
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
var selected = 0
let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
println(selectedIndex)
}
var touch = touches.anyObject() as UITouch
if touch.view is UILabel
{
var picker = ActionSheetStringPicker(title: "Select an attachment", rows: attachmentsList, initialSelection: 0, doneBlock: done, cancelBlock: nil, origin: touch.view)
picker.showActionSheetPicker()
}
}
The element responding to the touch is a UILabel, called attachmentLabel, which I would like to change the text of depending on the selection made in the ActionSheetStringPicker. However, when I get inside the done block, I get an EXC_BAD_ACCESS error whenever I try to access anything other than the three parameters that were passed in. And if I try to use the pickerDone function as a success action, it is only called if I define the function to not take any parameters.
All the examples I can find for the done block only print out the values of the parameters. I need an example for how to get the information from the picker back out to my UI.
I've tried to figure out how to capture variables for use in my closure, but I get the same EXC_BAD_ACCESS error. I tried capturing a local, but that didn't work:
var selected = 0
let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
selected = selectedIndex
}
So then I tried capturing self, but that doesn't work either:
let done: ActionStringDoneBlock = {[weak self](picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in
self?.attachmentLabel.text = self?.attachmentsList[selectedIndex]
return
}