回答
您可以將委託添加到您的控制器<UITabBarDelegate>
,
創建的TabBar編程
UITabBar * aTabBar;
與UITabBarItems
填充它,然後實現這個功能來處理選項卡上的觸摸切換視圖
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
}
這是代碼的簡要部分
@interface yourTabsViewController : UIViewController <UITabBarDelegate>
{
UITabBar * mTabBar;
NSMutableDictionary * mControllerPerTab;
}
@end
在您的實現:
- (void)viewDidLoad
{
mControllerPerTab = [[NSMutableDictionary alloc] init];
[mControllerPerTab setValue:controller forKey:@"aKey"];
UIImage *bImage = /*icon of tab*/;
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"title" image:bImage tag:/*a tag for your tab*/];
[tabBarItems addObject:item];
}
mTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 49/*tabbbar lenght*/ - 44/*navigationbar length if it exists*/, self.view.bounds.size.width ,49)];
[mTabBar setItems:tabBarItems];
mTabBar.delegate = self;
mTabBar.selectedItem = [tabBarItems objectAtIndex:0];
[self tabBar:mTabBar didSelectItem:[tabBarItems objectAtIndex:0]];
// Finally, add the tab controller view to the parent view
[self.view addSubview:mTabBar];
[super viewDidLoad];
}
然後你添加這個m方法來處理標籤的切換
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
int tag = [item tag];
/*I'm using the tag to identify wich coltroller to open*/
UIViewController * controller = [mControllerPerTab objectForKey:[NSString stringWithFormat:@"%d", tag]];
controller.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height - 49);
[self.view addSubview:controller.view];
[self.view addSubview:mTabBar];
[self.view autoresizesSubviews];
}
我正在使用故事板,並且無法以編程方式創建選項卡欄,因爲我沒有細分視圖的子類。難道它不能在Storyboard中完成嗎? – AlexR 2012-02-21 08:40:04
我不是很高級的使用故事板,但我認爲你應該做的是專注於添加一個tabBar到你的視圖,並忘記tabBarController,因爲不僅你不能繼承它,但它只能是第一個控制器在你的應用。所以嘗試添加tabBar並在你的代碼中添加委託和切換功能 – 2012-02-21 08:45:51
好吧,你是否願意處理旋轉(BarButtonItem添加和刪除到tabbarcontroller或每個視圖控制器中的工具欄? – AlexR 2012-02-21 08:55:20
- 1. UITabBarController和UINavigationController的子類化
- 2. ios子類化替代品
- 3. NativeWindow替代子類化
- 4. 製作替代UITabBarController
- 5. 子類或不子類的UITabBarController
- 6. 的UITabBarController子類&徽章
- 7. 子類的UITabBarController故事板
- 8. Django - 替代子類User?
- 9. UITabBarController的子類,替換了TabBar並有顯示問題
- 10. 春季子類化LocalValidatorFactoryBean的替代方案
- 11. 替代UIWebView顯示格式化帖子
- 12. 瞭解UITabBarController子類初始值設定項代碼Obj-C
- 13. 如何定義UITabBarController的子類?
- 14. UITabbarController和AppStore驗證的子類
- 15. UIView子類認爲它是一個UITabBarController
- 16. 使用子類的自定義UITabBarController
- 17. 子類UITabBarController來調整其框架
- 18. didSelectViewController爲的UITabBarController的子類不工作
- 19. 子類中的替代安裝程序
- 20. 子實體的替代鍵類型
- 21. 優化替代CGContextDrawImage
- 22. 替代APImage類?
- 23. 替代類圖
- 24. 父子表替代
- 25. 替代子查詢
- 26. 替代子查詢
- 27. Python:子類化frozenset不可迭代?
- 28. 在IOS6底部的黑色條代替UITabBarController
- 29. UITabbarController + UINavigationController,用UIToolbar代替標籤欄的詳細視圖
- 30. UITabBarController子類方法在子視圖控制器上不可用?
爲什麼你需要子類?爲什麼不使用標準標籤欄。在任何情況下[Composition](http://en.wikipedia.org/wiki/Object_composition)都可以替代繼承。 – beryllium 2012-02-21 08:26:13