我正在創建一個Rubymotion應用程序,我需要在導航欄中創建一個自定義按鈕。 我在控制器中運行代碼。如何將自定義按鈕添加到導航欄?
我使用這個代碼:
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
但運行時耙我得到這個錯誤:
Terminating app due to uncaught exception 'NoMethodError', reason: 'first_controller.rb:21:in `button': undefined method `setBackgroundImage' for #<UIBarButtonItem:0x6b3c1d0> (NoMethodError)
SOLUTION
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
感謝編輯@dylan! –
這工作得很好!謝謝! –
順便說一句,RubyMotion剛剛發佈了API文檔,因此您可以在Ruby中看到正確的方法,而不必使用Apple的Obj-C參考:http://www.rubymotion.com/developer-center/api/UIBarButtonItem.html#了setBackgroundImage%3AforState%3AbarMetrics%3A-instance_method –