Since your question did mention rmq, here's an rmq way of doing it:
In viewDidLoad
:
@hello_world_label = rmq.append(UILabel, :hello_world).get
@button = rmq.append(UIButton, :toggleable_button)
@button.on(:touch) do |sender|
sender.selected = !sender.selected?
end
Note that the toggle is achieved by querying the button's actual state. If you need to remember this for later use, you might want to save that in an instance variable.
In your stylesheet:
def toggleable_button(st)
st.frame = {t: 200, w: 100, h: 24}
st.image_normal = image.resource('toggle_me')
st.image_selected = image.resource('toggled')
end
Note the use of image_selected
. Well, this doesn't exist in rmq, but you can make that happen quite easily. If this is an rmq project, you'll have a stylers/ directory. In there, you should see ui_button_styler.rb. Here's the code to make the highlighted state a first-class citizen:
module RubyMotionQuery
module Stylers
class UIButtonStyler < UIControlStyler
def image_selected=(value)
@view.setImage(value, forState:UIControlStateSelected)
end
def image_selected
@view.imageForState UIControlStateSelected
end
end
end
end
As you can see, your controller code remains clean, the initial settings of the button are shifted to the stylesheet and you have neatly extended rmq to understand the selected state.
button.selected = !selected
where is the!selected
coming from? if I dobutton.selected = !button.selected
then I get an error – Ralf