rubymotion-community / BubbleWrap

Cocoa wrappers and helpers for RubyMotion (Ruby for iOS and OS X) - Making Cocoa APIs more Ruby like, one API at a time. Fork away and send your pull requests
Other
1.18k stars 208 forks source link

GestureRecognizer Helpers nulling out attributes inside callback block #465

Closed awernick closed 9 years ago

awernick commented 9 years ago

I'm not sure if my title is correct, but I stumbled upon a really strange behavior while using the GestureRecognizer.

I'm trying to add a long press recognizer to a MKMapView using BubbleWrap helpers. My code structure can be seen in the following gist: https://gist.github.com/dc7716a9c66987100539.git

When a long press is detected wishing the MKMapView, the block passed to when_pressed is executed as expected. The issue arises when add_pin_to_map tries to call the map view's addAnnotation method. My app crashes due to undefined method 'addAnnotation' for nil:NilClass (NoMethodError).

I tried checking to see if my MKMapView had been destroyed by garbage collection at some point in the method call, but that doesn't seem to be the case since I can comment out the call to addAnnotation in add_pin_to_map and still reference map_view back in the block (that is the reason why I have print statements for self and map_view in the block and in add_pin_to_map)

Does BubbleWrap block access to attributes when using callback helpers?

markrickert commented 9 years ago

Hey @awernick, it looks like your map_view variable is local instead of an instance var. initialize it as @map_view and reference it that way everywhere and you should be good.

Tangentially, may I recommend using ProMotion and ProMotion-map? I've already solved lots of these problems for you regarding maps and long presses. If you check out the readme, you'll see that you can completely customize the dropped pin - https://github.com/clearsightstudio/ProMotion-map#tap_to_addlength-float-target-object-action-selector-annotation-hash

Feel free to let me know if you have any questions about ProMotion-map in that github project or join the new Slack channel here: http://motioneers.herokuapp.com/

-Mark

awernick commented 9 years ago

@markrickert hahah truly sorry! I just got started with RubyMotion and I foolishly thought the compiler would prioritize methods over local variables. Fixed it by using self.map_view upon initialization. Thanks a bunch! And I will definitely check out ProMotion, it looks very promising.