有人知道为什么以下RubyMotion代码中的按钮在点击时不会触发say_something方法...
class HomeViewController < UIViewController
def loadView
self.view = UIImageView.alloc.init
end
def viewDidLoad
view.image = UIImage.imageNamed('background.png')
@action_button = UIButton.buttonWithType UIButtonTypeCustom
@action_button.frame = [[100, 100], [224, 229]]
@action_button.setBackgroundImage(UIImage.imageNamed("home_button_green.png"), forState: UIControlStateNormal)
@action_button.addTarget(self, action: :say_something, forControlEvents: UIControlEventTouchUpInside)
view.addSubview @action_button
end
def say_something
@alert_box = UIAlertView.alloc.initWithTitle("Hello World",
message:"Hi",
delegate: nil,
cancelButtonTitle: "ok",
otherButtonTitles:nil)
# Show it to the user
@alert_box.show
end
def viewDidAppear(animated)
end
end
答案 0 :(得分:2)
我找到了答案,需要告知视图允许用户交互......
view.userInteractionEnabled = true
这解决了问题,按钮现在按下并触发事件。