替代UITabBarController的子类

时间:2012-02-21 08:19:12

标签: objective-c ios uitabbarcontroller uisplitviewcontroller

似乎UITabBarController不应该是子类。您如何建议我在可旋转的DetailView中实现TabBarController?

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以向控制器添加<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];
}

然后添加此方法以处理选项卡的切换

- (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];
}