2012-04-24 39 views
0

當我在iPhone 4.3模擬器中使用此代碼時,我得到了這個錯誤,但是當它在iPhone 5模擬器上運行時,它的工作沒有錯誤。TabbarController的Tabbar顏色

代碼

UITabBarController *tabB = [[UITabBarController alloc] init]; 
tabB.tabBar.tintColor=[UIColor colorWithRed:124.0/255.0 green:150.0/255.0 blue:32.0/255.0 alpha:1.0]; 

tabB.tabBar.selectedImageTintColor=[UIColor colorWithRed:187.0/255.0 green:255.0/255.0 blue:38.0/255.0 alpha:1.0];    
tabB.viewControllers = [NSArray arrayWithObjects:hc,bt, nil]; 
[self.navigationController pushViewController:tabB animated:YES]; 

錯誤

-[UITabBar setTintColor:]: unrecognized selector sent to instance 0x78787c0 
2012-04-24 10:59:46.776 welcomeKidsWebsite[10357:12203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBar setTintColor:]: unrecognized selector sent to instance 0x78787c0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x01bcd5a9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x01d21313 objc_exception_throw + 44 
    2 CoreFoundation      0x01bcf0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x01b3e966 ___forwarding___ + 966 
    4 CoreFoundation      0x01b3e522 _CF_forwarding_prep_0 + 50 
    5 welcomeKidsWebsite     0x0005ac8e -[KidsWelcomeViewController GotoBooks:] + 622 
    6 UIKit        0x00e1f4fd -[UIApplication sendAction:to:from:forEvent:] + 119 
    7 UIKit        0x00eaf799 -[UIControl sendAction:to:forEvent:] + 67 
    8 UIKit        0x00eb1c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 
    9 UIKit        0x00eb07d8 -[UIControl touchesEnded:withEvent:] + 458 
    10 UIKit        0x00e43ded -[UIWindow _sendTouchesForEvent:] + 567 
    11 UIKit        0x00e24c37 -[UIApplication sendEvent:] + 447 
    12 UIKit        0x00e29f2e _UIApplicationHandleEvent + 7576 
    13 GraphicsServices     0x02528992 PurpleEventCallback + 1550 
    14 CoreFoundation      0x01bae944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    15 CoreFoundation      0x01b0ecf7 __CFRunLoopDoSource1 + 215 
    16 CoreFoundation      0x01b0bf83 __CFRunLoopRun + 979 
    17 CoreFoundation      0x01b0b840 CFRunLoopRunSpecific + 208 
    18 CoreFoundation      0x01b0b761 CFRunLoopRunInMode + 97 
    19 GraphicsServices     0x025271c4 GSEventRunModal + 217 
    20 GraphicsServices     0x02527289 GSEventRun + 115 
    21 UIKit        0x00e2dc93 UIApplicationMain + 1160 
    22 welcomeKidsWebsite     0x000024ac main + 188 
    23 welcomeKidsWebsite     0x000023e5 start + 53 
+1

因爲這setTintColor:被添加到ios5所以你沒有在ios4.3 – 2012-04-24 09:13:36

+0

@AalokParikh你應該把這個作爲答案,因爲它是答案! – jrturton 2012-04-24 09:17:17

+0

有解決方案嗎? – 2012-04-24 09:17:42

回答

2

的一個好辦法,使這個獨立的版本是:

if ([tabBarController.tabBar respondsToSelector:@selector(setTintColor:)]) { 
     [tabBarController.tabBar setTintColor:color]; 
} 

在iOS 5中,這將設置的TabBar tintcolor和更低版本它不會崩潰

-2

試試這個

tabB.tintColor=[UIColor colorWithRed:124.0/255.0 green:150.0/255.0 blue:32.0/255.0 alpha:1.0]; 
+0

我得到錯誤再次看到代碼 – 2012-04-24 09:15:43

0

按照documentationtintColor僅在iOS 5中可用。 0和更高版本。線索是4.3模擬器說unrecognized selector sent to instance這基本上意味着該方法不存在。

你需要做以下之一:

  • 設置你的最低要求的iOS 5.0。
  • 僅在iOS 5.0或更高版本上運行時才應用色調。
  • 使用兩種版本都支持的替代方法。

文件摘錄:

tintColor

着色顏色應用到標籤欄背景。 @屬性(非原子,保留)的UIColor * tintColor可用性

Available in iOS 5.0 and later. 
+0

你知道替代方法嗎? – 2012-04-24 09:30:36

+0

我以前使用過[this post](http://stackoverflow.com/questions/3339722/check-iphone-ios-version)這樣的方法來爲iOS 5上的用戶啓用色調,但取決於您的應用程序這可能不可行。 – GregularExpressions 2012-04-24 10:22:35