0

我正在构建一个 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;
}

我的红宝石代码:

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中设置这段代码真的正确吗?

4

1 回答 1

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'))
于 2012-11-16T15:33:09.847 回答