0

有谁知道为什么以下 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
4

1 回答 1

2

我找到了答案,视图需要被告知允许用户交互......

view.userInteractionEnabled = true

这解决了问题,现在按下按钮并触发事件。

于 2012-08-23T14:45:35.173 回答