0

Rubymotionアプリを作成していて、tabBarをカスタマイズしています。tabBarの背景としてカスタム画像を配置することができましたが、各タブに個別の画像を設定する必要があります。1つは押されたとき用、もう1つは押されていないとき用です。

私は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に設定するのは本当に正しいですか?

4

1 に答える 1

2

Objective-Cのこれらの行は1つの方法です。

[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 に答える