替代子类化UITabBarController [英] Alternative to subclassing UITabBarController

查看:94
本文介绍了替代子类化UITabBarController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It seems that UITabBarController should not be subclassed. How would you recommend that I implement a TabBarController in a rotatable DetailView?

谢谢!

推荐答案

您可以向控制器添加<UITabBarDelegate>的委托,

You can add to your controller a delegate to <UITabBarDelegate>,

以编程方式创建tabBar

create a tabBar programmatically

UITabBar * aTabBar;

UITabBar * aTabBar;

并用UITabBarItems填充 然后实现此功能以处理选项卡上的触摸以切换视图

and fill it with UITabBarItems and then implement this function to handle the touch on a tab to switch views

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

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

Then you add this method to handle the switching of tabs

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

这篇关于替代子类化UITabBarController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆