Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active December 14, 2015 23:08
Show Gist options
  • Save ddrscott/5163136 to your computer and use it in GitHub Desktop.
Save ddrscott/5163136 to your computer and use it in GitHub Desktop.
RubyMotion UIBarButtonItem Helper
# Example:
# self.navigationItem.leftBarButtonItems = [
# ui_bar_button_item('1'){App.alert('One')},
# ui_bar_button_item('2'){App.alert('Two')}
# ]
def ui_bar_button_item(title, &block)
item = UIBarButtonItem.alloc.initWithTitle(title, style: UIBarButtonItemStylePlain, target: block, action: :call)
item.instance_variable_set('@_target', block) # retain
item
end
@ddrscott
Copy link
Author

Line #8 instance_variable_set is critical because the block doesn't seem to be retained in UIBarButtonItem.

I'm not sure if this will create memory leaks or not :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment