【发布时间】:2012-06-19 06:39:44
【问题描述】:
有没有办法在调用另一个函数时通过 addTarget 调用传递参数?
我也尝试过 sender 方法 - 但这似乎也失败了。在不创建全局变量的情况下传递参数的正确方法是什么?
@my_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
@my_button.frame = [[110,180],[100,37]]
@my_button.setTitle("Press Me", forState:UIControlStateNormal)
@my_button.setTitle("Impressive!", forState:UIControlStateHighlighted)
# events
newtext = "hello world"
@my_button.addTarget(self, action:'buttonIsPressed(newtext)', forControlEvents:UIControlEventTouchDown)
view.addSubview(@my_button)
def buttonIsPressed (passText)
message = "Button was pressed down - " + passText.to_s
NSLog(message)
end
更新:
好的,这是一个带有实例变量的方法。
@my_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
@my_button.frame = [[110,180],[100,37]]
@my_button.setTitle("Press Me", forState:UIControlStateNormal)
@my_button.setTitle("Impressive!", forState:UIControlStateHighlighted)
# events
@newtext = "hello world"
@my_button.addTarget(self, action:'buttonIsPressed', forControlEvents:UIControlEventTouchDown)
view.addSubview(@my_button)
def buttonIsPressed
message = "Button was pressed down - " + @newtext
NSLog(message)
end
【问题讨论】:
标签: rubymotion