如何更改tabbar项目的状态图像?

时间:2012-11-16 13:46:53

标签: objective-c rubymotion

我正在构建一个Rubymotion应用程序,我正在自定义tabBar。 我设法将自定义图像作为tabBar的背景,但现在我需要 将单个图像设置为每个选项卡。一个用于按压时,一个用于按下时。

我正在关注NSScreencasts.com上的指南(针对objective-c),并且演示说明我应该使用此代码。但是当我在Ruby中尝试它时(我认为是正确的)我得到一个错误。

在Objective-C中:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Friends"
                                                        image:nil
                                                          tag:0];
        [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabbar-activity-selected.png"]
                      withFinishedUnselectedImage:[UIImage imageNamed:@"tabbar-activity.png"]];
    }
    return self;
}

我的Ruby代码:

class FirstController < UIViewController
  def viewDidLoad
    super

    view.backgroundColor = UIColor.whiteColor 

    self.tabBarItem = UITabBarItem.alloc.initWithTitle('Friends', image: nil, tag: 0)
    self.tabBarItem.setFinishedSelectedImage(UIImage.imageNamed('tabitem_selected.png'))
    self.tabBarItem.withFinishedUnselectedImage(UIImage.imageNamed('tabitem.png')) 
  end
end

错误:

first_controller.rb:8:in `viewDidLoad': undefined method `setFinishedSelectedImage' for #<UITabBarItem:0x6b71670> (NoMethodError)
    from app_delegate.rb:7:in `application:didFinishLaunchingWithOptions:'
2012-11-16 14:45:56.924 custom_tabbar[45679:f803] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'first_controller.rb:8:in `viewDidLoad': undefined method `setFinishedSelectedImage' for #<UITabBarItem:0x6b71670> (NoMethodError)

另外。在viewDidLoad中设置此代码是否真的正确?

1 个答案:

答案 0 :(得分:2)

Objective-C中的这些行是一种方法:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"tabbar-activity-selected.png"]
              withFinishedUnselectedImage:[UIImage imageNamed:@"tabbar-activity.png"]];

签名是:

- (void)setFinishedSelectedImage:(UIImage *)selectedImage 
     withFinishedUnselectedImage:(UIImage *)unselectedImage

因此对于RubyMotion,方法签名是:

setFinishedSelectedImage(image, withFinishedUnselectedImage:image)

为您转换为此内容:

self.tabBarItem.setFinishedSelectedImage(UIImage.imageNamed('tabitem_selected.png'),
                                         withFinishedUnselectedImage: UIImage.imageNamed('tabitem.png'))