我想在我的应用程序的一个视图中使用Tab栏,这是基于VIEW的应用程序。所以我拖了一个标签栏并添加了2个标签栏项目,使其完全为4。
我已将每个标签项标记为1,2,3,4,分别包含Myservices,History,RecentRequest和profile label。
类似地,我为每个选项卡添加了4个类,其名称为Myservices.h,.m,.xib,以及其他选项卡的名称与上面相同。
在上面这里,Tabs 3是UITableViewController的子类,profile选项卡是UIVIewController的子类。当我点击配置文件选项卡时它显示tableView,我也在其他视图中做了一些修改,但点击任何TAB它显示所有选项卡的相同屏幕所以任何人都可以打电话给我,我错了。
这是我的代码我在名为Homepage.h和.m的类中添加了此代码,并在homepage.xib中拖动了标签栏
// CODE homepage.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
// HomePage.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];
}
点击任何标签,视图没有变化,任何人都可以告诉我哪里出错了?
答案 0 :(得分:1)
由于
[self activateTab:1];
您选择选项卡索引1,这就是为什么每次获得相同选项卡时,您需要每次都传递选定的选项卡索引而不是修复。
我也无法选择任何选项卡栏方法来获取选择标签栏索引。
答案 1 :(得分:0)
答案 2 :(得分:0)
经过一次又一次的代码后,我发现我遗漏了一行代码[self activateTab:item.tag];
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method.
{
[self activateTab:item.tag];
}
我编辑了我的代码并且工作正常。