0

Rubymotion アプリを作成しており、Navbar にカスタム ボタンを作成する必要があります。コントローラーでコードを実行しています。

私はこのコードを使用しています:

button = UIBarButtonItem.alloc.init
    button.title = 'Add'
    button.style = UIBarButtonItemStylePlain
    button.target = self
    button.action = 'performAdd'
    button.setBackgroundImage(checkInImage, forState:UIControlStateNormal, forBarMetrics:UIBarMetricsDefault)
    button.setBackgroundImage(checkInPressed, forState:UIControlStateHighlighted, forBarMetrics:UIBarMetricsDefault)
    self.navigationItem.rightBarButtonItem = button

しかし、rake を実行すると次のエラーが発生します。

Terminating app due to uncaught exception 'NoMethodError', reason: 'first_controller.rb:21:in `button': undefined method `setBackgroundImage' for #<UIBarButtonItem:0x6b3c1d0> (NoMethodError)

解決

    button = UIBarButtonItem.alloc.init
        button.title = 'Add'
        button.target = self
        button.action = 'performAdd'
        button.setBackgroundImage(checkInImage, forState:UIControlStateNormal, barMetrics:UIBarMetricsDefault)
        button.setBackgroundImage(checkInPressed, forState:UIControlStateHighlighted, barMetrics:UIBarMetricsDefault)
self.navigationItem.rightBarButtonItem = button
4

2 に答える 2

2

私の読書によると、それbarMetricsはではなく、forBarMetricsです。それで:

setBackgroundImage(checkInImage,
                   forState:UIControlStateNormal, barMetrics:UIBarMetricsDefault)
于 2012-11-16T18:02:25.513 に答える
1

私は「RubyMotion」の初心者ですが、まだあなたを助けようとしています.どこかで正しくない場合はご容赦ください.

編集: クラッシュ ログTerminating app due to uncaught exception 'NoMethodError', reason: 'first_controller.rb:21:inボタンを表示する': undefined method setBackgroundImage' for #<UIBarButtonItem:0x6b3c1d0> (NoMethodError). backgroundImage を設定するための正しいメソッドを呼び出していView controllerないので、そのような例外が発生するのはなぜだと思いますか。

そして、私は正確な理由が方法である可能性があると思います.setBackgroundImageそれは正しい方法ではないと思います.それはそうあるべきです .次にbackgroundImage、あなたのViewController.

  button.backgroundImage(checkInPressed, forState:UIControlStateHighlighted, forBarMetrics:UIBarMetricsDefault).

これは、その1行を変更するだけのコードです。

  button = UIBarButtonItem.alloc.init
  button.title = 'Add'
  button.style = UIBarButtonItemStylePlain
  button.target = self
  button.action = 'performAdd'
  button.backgroundImage(checkInImage, forState:UIControlStateNormal, forBarMetrics:UIBarMetricsDefault)
  button.backgroundImage(checkInPressed, forState:UIControlStateHighlighted, forBarMetrics:UIBarMetricsDefault)
  self.navigationItem.rightBarButtonItem = button

これが同じリンクです。このリンクのビューを取得する必要があります。

私はあなたがいくつかのアイデアを得ることができることを願っています..

于 2012-11-16T16:22:05.313 に答える