Search code examples
rubymotion

Set an action for a teacup styled button in ruby motion


if i am creating my button in my pro-motion screen:

add button = UIButton.new,
  stylename: :my_button,            # Teacup
  resize: [ :left, :right, :top ], # ProMotion
  frame: CGRectMake(25, 250, 90, 90)

How can I add an action to my button?

The action should look something like this:

addTarget(self, action: :an_action,
                    forControlEvents:UIControlEventTouchUpInside)

Solution

  • Here's a simple way:

    button = add UIButton.new, {
      stylename: :my_button,            # Teacup
      resize: [ :left, :right, :top ], # ProMotion
      frame: CGRectMake(25, 250, 90, 90),
      :"addTarget:action:forControlEvents:" => [ self, :an_action, UIControlEventTouchUpInside ]
    }
    

    You can just pass in an array and the add method will splat it into args. It's essentially the same as:

    send(:"addTarget:action:forControlEvents:", self, :an_action, UIControlEventTouchUpInside)
    

    By the way, you need to put the add call on the other side of the assign, since the hash is the second argument of the add method.