生活随笔
收集整理的這篇文章主要介紹了
UITabBarController使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注:應原創作者要求,特注明轉載自http://blog.csdn.net/hb308102796/article/details/6445227,請各位尊重別人的勞動果實!
[cpp]?view plaincopy
-(id)init?{?? ????if?([super?init]?!=?nil)?{?? ????????UITabBarItem?*item?=?[[UITabBarItem?alloc]?initWithTitle:@"asdfsadf"?image:[UIImage?imageNamed:@"WWAN5.png"]?tag:1];?? ????????self.tabBarItem?=?item;?? ????????[item?release];?? ????}?? ????return?self;?? }??
我很少寫關于IOS的文章,寫這篇完全是因為網絡上copy,paste的文章太多,將我誤導,搞的我花了半天時間才會用這控件,最后還是看了外國一個英文貼子,才會用。因此寫了這篇供后學之人學習加快吧,也希望大家在寫文章時,不要千篇一律的復制、粘貼。我們是軟件工程師,而不是復制、粘貼工程師。
該文章內容展示效果如下圖:
接下來,你將看到完全用代碼實現的tab bar選項卡切換效果。下面開始。
準備工作,創建一個項目名為ViewSwitcher(你可以選擇基于View-based Application或Window-based Application,只不過選擇后者的話,要自己創建一個view controller罷了,我是選擇了第一個)。
在此,我不會教大家只在ViewSwitcherAppDelegate中去創建UITabBarController的實例(這種方式網上到處都是),我要教大家如何在自己的view controller中創建UITabBarController實例。
下一步,創建兩個view controller類,我這里命名為BlueViewController和YellowViewController,當然,它們都是UIViewController的子類。
接著,在ViewSwitcherViewController的viewDidLoad方法中,代碼如下:
[cpp]?view plaincopy
tabBar?=?[[UITabBarController?alloc]?init];?? tabBar.delegate?=?self;?? blueViewController?=?[[BlueViewController?alloc]?init];?? yellowViewController?=?[[YellowViewController?alloc]?init];?? NSArray?*viewControllerArray?=?[NSArray?arrayWithObjects:blueViewController,yellowViewController,nil];?? tabBar.viewControllers?=?viewControllerArray;?? tabBar.view.frame?=?CGRectMake(0,?0,?self.view.frame.size.width,?self.view.frame.size.height);?? [self.view?addSubview:tabBar.view];?? [viewControllerArray?release];??
?
其中tabBar是在.h文件中聲明的UITabBarController對象實例。這樣運行看看吧。
你會看到為什么兩個按鈕是黑色的呢,沒有字呢?沒錯,因為我們還沒有寫這部分代碼。設置tab bar標簽的圖片或文字,可以在它的子view controller中做(這么說或許不是很恰當,因為官方可不這么叫),在這里,我是寫在blueViewController和yellowViewController中的,重寫它們的init方法,將它們的tabBarItem成員賦值,代碼如下:
[css]?view plaincopy
-(id)init?{?? ????if?([super?init]?!=?nil)?{?? ????????UITabBarItem?*item?=?[[UITabBarItem?alloc]?initWithTitle:@"asdfsadf"?image:[UIImage?imageNamed:@"WWAN5.png"]?tag:1];?? ????????self.tabBarItem?=?item;?? ????????[item?release];?? ????}?? ????return?self;?? }??
?
運行進來 ,你將看到新的效果。
那么,那個在item上的小紅圈提示是怎么來的呢??我們實現UITabBarDelegate中的- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController方法,代碼如下:
[cpp]?view plaincopy
-?(void)tabBarController:(UITabBarController?*)tabBarController?didSelectViewController:(UIViewController?*)viewController{?? ?? ?????? ????viewController.tabBarItem.badgeValue?=?[NSString?stringWithFormat:@"%d",80];?? ?? }??
?
聰明的人,應該不需要我一行行的去講解代碼吧!
最后,轉載請注明出處,謝謝!
總結
以上是生活随笔為你收集整理的UITabBarController使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。