1

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
}
skywinder
  • 21,291
  • 15
  • 93
  • 123
aranha
  • 43
  • 4

1 Answers1

0

I'm owner of ActionSheetPicker. I didn't try work with touchesBegan overriding, but it should be the same.

I try to wrap your code from touchesBegan to @IBAction and it works (prints index after done is clicked):

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

        var picker = ActionSheetStringPicker(title: "Select an attachment", rows: ["One", "Two", "A lot"], initialSelection: 0, doneBlock: done, cancelBlock: nil, origin: sender.superview!.superview)

        picker.showActionSheetPicker()

    }

And here is example to run your function from done block:

func pickerDone()
{
    println("Here I am!")
}

@IBAction func functionInvokeByDoneCicked(sender: UIButton) {
    var picker = ActionSheetStringPicker(title: "title", rows: ["1","2"], initialSelection: 0, target: self, successAction: "pickerDone", cancelAction: nil, origin: sender.superview!.superview)

    picker.showActionSheetPicker()
}

Probably there is a problem with your origin parameter. Check that touch.view is the main view.

skywinder
  • 21,291
  • 15
  • 93
  • 123
  • I am already able to print from the ActionStringDoneBlock or from the done block as you have done here. The problem is that I'm unable to do anything BUT print. I can't access any variables inside those functions other than the ones passed in, so I can't figure out how to get any information back out to my UI. For example, I can print the selected index just fine, but what I need to do is get the value of the selected index back out to my UILabel. – aranha Jan 27 '15 at 17:10
  • @aranha the same way as you do it from other blocks or callback methods. 1) create outlet for your text field `myTextField` 2) replace `println` with `self.myTextField.text = self.arrayWithStrings[selectedIndex]` for example. Check example project in https://github.com/skywinder/ActionSheetPicker-3.0 You can find examples of this action there. – skywinder Jan 28 '15 at 07:07
  • I have a label and a list of strings: `@IBOutlet weak var attachmentLabel: UILabel! var attachmentsList = [String]()` The list is populated elsewhere. If I make action like this: `let done: ActionStringDoneBlock = {[weak self](picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in var index: Int = selectedIndex self?.attachmentLabel.text = self?.attachmentsList[index] }` I crash with a EXC_BAD_ACCESS error. Sorry, I tried to format this nicely, but I can't figure out how to get the line breaks to appear. – aranha Jan 29 '15 at 16:36
  • @aranha it's better to open issue in GitHub [repository](https://github.com/skywinder/ActionSheetPicker-3.0/issues) and continue our discussion there with nice-formatted comments. – skywinder Jan 29 '15 at 17:21
  • I have the same problem with the EXC_BAD_ACCESS error when trying to access a variable in the "done" block. Do you already have a solution for this problem? – ManfredP Feb 12 '15 at 14:28
  • @ManzMoody please, open issue on github page and describe in details your case – skywinder Feb 12 '15 at 15:15