【问题标题】:On click on Tab bar item view is not changing in iphone点击标签栏项目视图在 iphone 中没有改变
【发布时间】: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


【解决方案1】:

因为 [self activateTab:1];

您选择选项卡索引 1,这就是为什么每次获得相同选项卡时都需要每次都传递选定的选项卡索引而不是修复。

我也找不到任何选择标签栏的方法来获取选择标签栏索引。

【讨论】:

  • 是的,我试过如果我更改标签索引 4 它只显示个人资料页面视图。那么我应该在上面的代码中更改什么??
  • 我已经编辑了上面的代码。我在上面的代码中也添加了这个方法 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSLog(@"didSelectItem : %d", item.tag);但我没有得到添加什么来选择特定视图
  • 为此您需要设置标签栏的标签。选择标签栏并设置每个标签的标签属性。像 1,2,3 什么的。
  • 你也把它和interface builder连接起来了?
【解决方案2】:

【讨论】:

  • 以上链接是标签栏应用程序,我在基于视图的应用程序中使用上述代码。您提供的标签栏应用程序链接与上述代码没有链接
【解决方案3】:

以下代码用于创建标签栏及其视图,我们也可以使用以下代码动态更改视图。

AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    UIViewController *viewController1 = [[viewcontroller1 alloc] initWithNibName:@"viewcontroller1" bundle:nil];
    UIViewController *viewController2 = [[viewcontroller2 alloc] initWithNibName:@"viewcontroller2" bundle:nil];
    UIViewController *viewController3 = [[viewcontroller3 alloc] initWithNibName:@"viewcontroller3" bundle:nil];


 UIViewController *viewController4 = [[viewcontroller3 alloc] initWithNibName:@"viewcontroller4" bundle:nil];


    UIViewController *viewController5 = [[Deliveryviewcontroller alloc] initWithNibName:@"viewcontroller5" bundle:nil];



            delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, nil];

            delegate.tabBarController.selectedIndex = 0;

            delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:delegate.tabBarController animated:YES];

下面要做的事情进入委托方法

@property (strong, nonatomic) UITabBarController *tabBarController;

并合成那个UITabbarController

@synthesize tabBarController = _tabBarController;

【讨论】:

    【解决方案4】:

    一遍又一遍地查看代码后,我发现我漏掉了一行代码[self activateTab:item.tag];

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method. 
    {
        [self activateTab:item.tag];
    }
    

    我已经编辑了我的代码,它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      相关资源
      最近更新 更多