Class: UIControl

Inherits:
Object show all
Defined in:
lib/ios/sugarcube-events/uicontrol.rb

Overview

Additions to UIControl to support jQuery-style on and off methods.

Instance Method Summary collapse

Instance Method Details

#off(*events) ⇒ Object

Removes all events that were bound with on. See symbol.rb for the uicontrolevent constant aliases.

Examples:

button.off(:touch)
button.off(:touch_up_outside, :touch_cancel)
button.off  # all events

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ios/sugarcube-events/uicontrol.rb', line 33

def off(*events)
  if events.length == 0
    events = sugarcube_callbacks.keys
  end

  events.each do |event|
    event = event.uicontrolevent if event.respond_to?(:uicontrolevent)

    sugarcube_callbacks(event).each do |handler|
      self.removeTarget(handler, action:'call:event:', 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.

Examples:

button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
button.on(:touch) { my_code }
button.on(:touch_up_outside, :touch_cancel) { my_code }
# up to two arguments can be passed in
button.on(:touch) { |sender,touch_event| my_code }

13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ios/sugarcube-events/uicontrol.rb', line 13

def on(*events, &block)
  handler = SugarCube::UIControlCallbackHelper.new(block)

  events.each do |event|
    event = event.uicontrolevent if event.respond_to?(:uicontrolevent)

    sugarcube_callbacks(event).push(handler)
    self.addTarget(handler, action:'call:event:', forControlEvents:event)
  end

  self
end

#trigger(*events) ⇒ Object

Useful during testing, or to simulate a button press.

Examples:

button.trigger(:touch)
button.trigger(:touch_drag_outside, :touch_drag_exits)

55
56
57
58
59
60
61
62
# File 'lib/ios/sugarcube-events/uicontrol.rb', line 55

def trigger(*events)
  event_mask = 0
  events.each do |event|
    event = event.uicontrolevent if event.respond_to?(:uicontrolevent)
    event_mask |= event
  end
  sendActionsForControlEvents(event_mask)
end