Class: UIControl

Inherits:
Object
  • Object
show all
Defined in:
lib/sugarcube/uicontrol.rb

Instance Method Summary collapse

Instance Method Details

#off(*events) ⇒ Object

Removes all events that were bound with ‘on`.

Examples:

button.off(:touch)
button.off(:touchupoutside, :touchcancel)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sugarcube/uicontrol.rb', line 32

def off(*events)
  events.each do |event|
    event = event.uicontrolevent unless Fixnum === event

    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

Examples:

button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
button.on(:touch) { my_code }
button.on(:touchupoutside, :touchcancel) { my_code }


16
17
18
19
20
21
22
23
24
25
# File 'lib/sugarcube/uicontrol.rb', line 16

def on(*events, &block)
  events.each do |event|
    event = event.uicontrolevent unless Fixnum === event

    sugarcube_callbacks[event].push block
    addTarget(block, action: :call, forControlEvents:event)
  end

  self
end

#sugarcube_callbacksObject

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