2012-11-16 99 views
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; 
} 

我的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中設置此代碼是否真的正確?

回答

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')) 
+0

謝謝你,那太棒了! –

+0

我應該在app_delegate中設置這些嗎?或者在控制器中?哪一個是正確的方法?如果我應該使用控制器,有什麼方法? –

相關問題