如果我在我的专业动态屏幕上创建我的按钮:
add button = UIButton.new,
stylename: :my_button, # Teacup
resize: [ :left, :right, :top ], # ProMotion
frame: CGRectMake(25, 250, 90, 90)
如何在按钮上添加动作?
行动看起来像这样:
addTarget(self, action: :an_action,
forControlEvents:UIControlEventTouchUpInside)
答案 0 :(得分:2)
这是一个简单的方法:
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 ]
}
您可以传入一个数组,add
方法会将其展开为args。它基本上与:
send(:"addTarget:action:forControlEvents:", self, :an_action, UIControlEventTouchUpInside)
顺便说一句,你需要将add
调用放在assign的另一端,因为hash是add
方法的第二个参数。