Sha256: 628dfb14a50b2cb11cff4da17b1aae4d6db3ed0f87e11c72cfabe3b6ab1244d2

Contents?: true

Size: 1.18 KB

Versions: 10

Compression:

Stored size: 1.18 KB

Contents


class UIControl

  # 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.
  def sugarcube_callbacks
    @sugarcube_callbacks ||= Hash.new { |hash, key| hash[key] = [] }
  end

  # Add event handlers to UIControls
  #
  # @example
  #   button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
  #   button.on(:touch) { my_code }
  #   button.on(:touchupoutside, :touchcancel) { my_code }
  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

  # Removes all events that were bound with `on`.
  #
  # @example
  #   button.off(:touch)
  #   button.off(:touchupoutside, :touchcancel)
  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

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sugarcube-0.2.3 lib/sugarcube/uicontrol.rb
sugarcube-0.2.2 lib/sugarcube/uicontrol.rb
sugarcube-0.2.1 lib/sugarcube/uicontrol.rb
sugarcube-0.2.0 lib/sugarcube/uicontrol.rb
sugarcube-0.1.6 lib/sugarcube/uicontrol.rb
sugarcube-0.1.5 lib/sugarcube/uicontrol.rb
sugarcube-0.1.4 lib/sugarcube/uicontrol.rb
sugarcube-0.1.3 lib/sugarcube/uicontrol.rb
sugarcube-0.1.2 lib/sugarcube/uicontrol.rb
sugarcube-0.1.1 lib/sugarcube/uicontrol.rb