Sha256: eb0720f52a8a0d82ecf175886a28fdace895e0fe140ef830150710a25ab90f27

Contents?: true

Size: 1.18 KB

Versions: 27

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

27 entries across 27 versions & 1 rubygems

Version Path
sugarcube-0.9 lib/sugarcube/uicontrol.rb
sugarcube-0.8.7 lib/sugarcube/uicontrol.rb
sugarcube-0.8.6 lib/sugarcube/uicontrol.rb
sugarcube-0.8.5 lib/sugarcube/uicontrol.rb
sugarcube-0.8.4 lib/sugarcube/uicontrol.rb
sugarcube-0.8.3 lib/sugarcube/uicontrol.rb
sugarcube-0.8.2 lib/sugarcube/uicontrol.rb
sugarcube-0.8.1 lib/sugarcube/uicontrol.rb
sugarcube-0.8 lib/sugarcube/uicontrol.rb
sugarcube-0.7.9 lib/sugarcube/uicontrol.rb
sugarcube-0.7.7 lib/sugarcube/uicontrol.rb
sugarcube-0.7.6 lib/sugarcube/uicontrol.rb
sugarcube-0.7.4 lib/sugarcube/uicontrol.rb
sugarcube-0.7.3 lib/sugarcube/uicontrol.rb
sugarcube-0.7.2 lib/sugarcube/uicontrol.rb
sugarcube-0.7.1 lib/sugarcube/uicontrol.rb
sugarcube-0.7 lib/sugarcube/uicontrol.rb
sugarcube-0.6.1 lib/sugarcube/uicontrol.rb
sugarcube-0.6 lib/sugarcube/uicontrol.rb
sugarcube-0.5 lib/sugarcube/uicontrol.rb