【发布时间】:2012-01-25 06:27:34
【问题描述】:
我想在我的应用程序的一个视图中使用标签栏,即基于视图的应用程序。所以我拖了一个标签栏并添加了 2 个标签栏项目,使其总共 4 个。
我已经分别用 Myservices、History、RecentRequest 和 profile 标签标记了每个标签项 1、2、3、4。
同样,我为每个选项卡添加了 4 个类,名称为 Myservices.h、.m、.xib,其他选项卡的名称也与上述相同。
在上面的选项卡 3 是 UITableViewController 的子类,而配置文件选项卡是 UIVIewController 的子类。当我单击配置文件选项卡时,它显示 tableView 并且我也在其他视图中进行了一些修改,但是单击任何选项卡时,它显示所有选项卡的相同屏幕,所以任何人都可以告诉我我哪里出错了。
这是我的代码,我在名为 Homepage.h 和 .m 的类中添加了这段代码,并且标签栏被拖到 homepage.xib 中
//代码 主页.h
IBOutlet UITabBar *myTabBar;
UIViewController *myServicesViewController;
UIViewController *historyViewController;
UIViewController *recentRequestViewController;
UIViewController *profileViewController;
UIViewController *currentViewController;
@property (nonatomic, retain) IBOutlet UITabBar *myTabBar;
@property (nonatomic, retain) UIViewController *myServicesViewController;
@property (nonatomic, retain) UIViewController *historyViewController;
@property (nonatomic, assign) UIViewController *currentViewController;
@property (nonatomic, assign) UIViewController *profileViewController;
@property (nonatomic, retain) UIViewController *recentRequestViewControll
//主页.m
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
[self activateTab:item.tag];
}
-(void)activateTab:(int)index
{
switch (index) {
case 1:
if (myServicesViewController == nil) {
self.myServicesViewController=
[[MyServices alloc] initWithNibName:nil bundle:nil];
}
[self.view insertSubview:myServicesViewController.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = myServicesViewController;
break;
case 2:
if (historyViewController == nil) {
self.historyViewController =
[[History alloc] initWithNibName:nil bundle:nil];
}
[self.view insertSubview:historyViewController.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = historyViewController;
break;
case 3:
if (recentRequestViewController == nil) {
self.recentRequestViewController =
[[RecentRequest alloc] initWithNibName:nil bundle:nil];
}
[self.view insertSubview:recentRequestViewController.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = recentRequestViewController;
break;
case 4:
if (profileViewController == nil) {
self.profileViewController =
[[Profile alloc] initWithNibName:nil bundle:nil];
}
[self.view insertSubview:profileViewController.view belowSubview:myTabBar];
if (currentViewController != nil)
[currentViewController.view removeFromSuperview];
currentViewController = profileViewController;
break;
default:
break;
}
}
-(void)viewDidLoad
{
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: @"bg.jpg"]];
self.view.backgroundColor = background;
[myTabBar setSelectedItem:[myTabBar.items objectAtIndex:0]];
[self activateTab:1];
[super viewDidLoad];
}
单击视图没有改变的任何选项卡,谁能告诉我哪里出错了??
【问题讨论】:
-
我错过了一行代码,即 [self activateTab:item.tag];在 (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 方法中。我已经编辑了我的代码,它工作正常......
-
你能告诉我你在哪里为每个标签设置标签,无论它在视图中会出现吗??
标签: objective-c ios ios5 uitabbar