skywinder / ActionSheetPicker-3.0

Quickly reproduce the dropdown UIPickerView / ActionSheet functionality on iOS.
http://skywinder.github.io/ActionSheetPicker-3.0
BSD 3-Clause "New" or "Revised" License
3.4k stars 740 forks source link

How to works with ActionStringDoneBlock in Swift? #137

Closed manzznam closed 9 years ago

manzznam commented 9 years ago

I alwas get this error when i try to write to the "index" variable in this "done" block. I took the code from the Swift Example. A println works inside the block but access to any variable doesn't.

@IBAction func pickerTouched(sender: UIButton) { var index = 0

    let done: ActionStringDoneBlock = {(picker: ActionSheetStringPicker!, selectedIndex: NSInteger!, selectedValue : AnyObject!) in            
        index = selectedIndex
        return
    }

    ActionSheetStringPicker.showPickerWithTitle("Nav Bar From Picker", rows: ["One", "Two", "A lot"], initialSelection: 1, doneBlock: done, cancelBlock: {ActionStringCancelBlock in return }, origin: sender.superview!.superview)
}

screenshot 2015-02-12 16 30 13

skywinder commented 9 years ago

Hi, please, check your origin. I belive it should be not sender.superview!.superview (most likely it nil) but sender.superview.

manzznam commented 9 years ago

Sorry that doesn't work neither. In my project i use a ContainerViewController as the window!.rootViewController. In the container i create a navigationController: mainNavigationController = UINavigationController(rootViewController: mainViewController). The mainViewController contains the Button for displaying the picker. Probably my views are different from the example and so is my origin then. But how do i know which is the right origin? Has the origin to be the same view where the picker is shown?

skywinder commented 9 years ago

Usually origin should be just the view, that call this picker. Such complex sender.superview!.superview expression should be only when you present picker on iPad from tableviewcell - to set correct position of the picker.

So, try to use just origin in your case. And let me know, if you still this error. Also, check, that you sender is not nil!

manzznam commented 9 years ago

What do you exactly mean with just use "origin"? I mean what parameter should i use for "origin"?

skywinder commented 9 years ago

Any UIView on the screen. In your case sender.view

manzznam commented 9 years ago

In my case sender is UIButton and does not have a property "sender.view". I tried also "view" and "self.view" as origin but i get the same error...

skywinder commented 9 years ago

oops, i mean just sender (cuz UIButton is subclass of view). Is it works?

manzznam commented 9 years ago

No that doesn't work either. I just tried to access a variable in your original Swift-Example project (fresh checkout) and there i get the same error. Could you provide some sample code how i can use the selectedIndex of the Picker in my ViewController?

skywinder commented 9 years ago

Yes, of course! You can fins a lot of examples in that repo. Just clone this repo and open xcworspace file.

manzznam commented 9 years ago

I only found doneBlock: {ActionStringDoneBlock in return} in your Swift sample code. But there is no example with a "programmed out" block of the doneBlock. I mean how can i write a correct done block in which i set the selected index e.g. self.selectedIndex = selectedIndex

loomtronic commented 9 years ago

I am getting the same thing. Would be great to have a clear, working example of how to get the value from the picker

skywinder commented 9 years ago

@loomtronic is this error exists in example project? Can you point it out? Because I'm confused and have no idea, how to reproduce it.

loomtronic commented 9 years ago

I haven't tried it in the example project... the example project I have not seen any Swift examples for this code. The Swift examples only have doneBlock: {ActionStringDoneBlock in return}, not the code @manzznam is using

manzznam commented 9 years ago

Yes that was my question as well. I only need some sample Swift code how to get the selected Index in that doneBlock.

skywinder commented 9 years ago

@manzznam, @loomtronic Yes, you're right! It's a good question, thank you! It's fucking closure syntax! Spend an hour to figure out, how to do it! Check examples now.

manzznam commented 9 years ago

Thank you, now it's working. Great job