可以让UIBarButtonItem的行动起作用

时间:2014-05-02 08:14:41

标签: cocoa-touch rubymotion

我有一个控制器和一个按钮。单击该按钮时,我想显示UIAlertView。我无法工作的是UIBarButtonItem操作参数。

在我的控制器中

class TapController < UIViewController
  def alert
    alert = UIAlertView.new
    alert.message = "It's working"
    alert.show
  end

  def viewDidLoad
    super
    self.view.backgroundColor = UIColor.whiteColor
    right_button = UIBarButtonItem.alloc.initWithTitle("Go Deeper",
                                                       style:UIBarButtonItemStyleBordered,
                                                       target:self,
                                                       action:'alert')
    self.navigationItem.rightBarButtonItem = right_button
  end
end

我的app/app_delegate.rb

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    #UIScreen is the display the app runs on
    @window = UIWindow.new.initWithFrame(UIScreen.mainScreen.bounds)
    @window.makeKeyAndVisible

    controller = TapController.alloc.initWithNibName(nil, bundle: nil)
    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(controller)

    true
  end
end

所以它很简单,但是UIBarButtonItem的目标或动作属性不能按预期工作。

1 个答案:

答案 0 :(得分:1)

这是你的问题。在您的应用代理中,您需要在alloc上拨打new而不是UIWindow

@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)