Class: UIControl
Instance Method Summary collapse
-
#off(*events) ⇒ Object
Removes all events that were bound with
on
. -
#on(*events, &block) ⇒ Object
Add event handlers to UIControls.
-
#sugarcube_callbacks ⇒ Object
event blocks need to be retained, and the addTarget method explicitly does not retain
target
.
Instance Method Details
#off(*events) ⇒ Object
Removes all events that were bound with on
. See symbol.rb for the
uicontrolevent constant aliases.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sugarcube/uicontrol.rb', line 35 def off(*events) if events.length == 0 events = sugarcube_callbacks.keys end events.each do |event| event = event.uicontrolevent unless event.is_a? Fixnum sugarcube_callbacks[event].each do |block| self.removeTarget(block, action: :call, forControlEvents:event) end sugarcube_callbacks.delete(event) end self end |
#on(*events, &block) ⇒ Object
Add event handlers to UIControls. See symbol.rb for the uicontrolevent constant aliases.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sugarcube/uicontrol.rb', line 17 def on(*events, &block) events.each do |event| event = event.uicontrolevent unless event.is_a? Fixnum sugarcube_callbacks[event].push block addTarget(block, action: :call, forControlEvents:event) end self end |
#sugarcube_callbacks ⇒ Object
event blocks need to be retained, and the addTarget method explicitly does
not retain target
. This makes sure that callbacks are retained by
pushing the block onto a stack.
6 7 8 |
# File 'lib/sugarcube/uicontrol.rb', line 6 def sugarcube_callbacks @sugarcube_callbacks ||= Hash.new { |hash, key| hash[key] = [] } end |