【问题标题】:Adding UITabBarItem to a UITabBar in a UIViewController将 UITabBarItem 添加到 UIViewController 中的 UITabBar
【发布时间】:2011-12-27 17:17:35
【问题描述】:

所以我有一个UIViewController,在视图中有一个UITabBar。我想下拉 JSON 并决定要添加到选项卡栏的选项卡,因此我需要能够在运行时选择选项卡。我想使用UITabBarController setViewControllers:animated: 方法向其中添加视图控制器列表。但是,由于我在视图控制器中执行此操作,因此我不知道如何访问选项卡栏的视图控制器。这是我的代码...

标题

#import <UIKit/UIKit.h>

@interface HealthTicketTabController : UIViewController <UITabBarDelegate, UITabBarControllerDelegate> {
    NSArray *viewControllers;
    IBOutlet UITabBar *tabBar;
    UIViewController *selectedViewController;

    // How do I link this the the tabBar's view controller???
    UITabBarController *tabBarController;
}

@property (nonatomic, retain) NSArray *viewControllers;
@property (nonatomic, retain) IBOutlet UITabBar *tabBar;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) UIViewController *selectedViewController;

@end

来源

- (id)init
{
    self = [super initWithNibName:@"HealthTicketTabView" bundle:nil];
    if (self)
    {   
        //Controllers for the tab view
        HealthCardController *card = [[HealthCardController alloc] init];
        MedicalExpensesController *medical = [[MedicalExpensesController alloc] init];

        //Tab bar items to be displayed in the tab bar
        card.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
        medical.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1];

        NSArray *array = [[NSArray alloc] initWithObjects:card, medical, nil];

        //Set the tab bar's view controllers to the list
        tabBarController.viewControllers = [NSArray arrayWithObjects:card, medical, nil];

        [array release];
        [card release];
        [medical release];
    }

    return self;
}

【问题讨论】:

    标签: iphone objective-c ios uitableview uiviewcontroller


    【解决方案1】:

    UIViewController 已经有一个 tabBarController 属性。您的头文件中不需要一个。这就是您可以访问 UITabBarController 的方式。

    【讨论】:

      【解决方案2】:

      发现由于我使用UIViewController 来控制UITabBar,因此我需要将标签栏的委托设置为当前的UIViewController,并在当前控制器中处理标签事件。

      将 TabBarItems 添加到 TabBar

      [self.tabBar setItems: tabBarItems animated:TRUE];
      

      在 ViewController 之间切换

      - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
      {
          UIViewController *temp = [viewControllers objectAtIndex:item.tag];
          [self.selectedViewController.view removeFromSuperview];
          [self.view addSubview:temp.view];
          self.selectedViewController = temp;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多