我正在创建一个 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
但是在运行 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