代码之家  ›  专栏  ›  技术社区  ›  Daddy

以编程方式设置自定义UitabarItem?

  •  2
  • Daddy  · 技术社区  · 15 年前

    在iOS中,TabbarController中的Tabbar属性是只读的。如何将自定义项与特定的视图控制器关联?如何访问Tabbar中的uitabaritems?

    这样地

    CustomView *custom = [[CustomView alloc] init];
    UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:@"Custom" image:[UIImage imageNamed:@"custom.png"] tag:0];
    SecondView *second = [[SecondView alloc] init];
    UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:@"Next" image:[UIImage imageNamed:@"next.png"] tag:1];
    NSArray *views = [NSArray arrayWithObjects:custom,second,nil];
    [tabBarController setViewControllers:views];
    //how do I set the individual TabBarItems (customTab,secondTab) to be associated
    //with the views in question?  tabBarController.tabBar is read only
    
    1 回复  |  直到 8 年前
        1
  •  5
  •   sigsegv    15 年前

    在每个视图控制器中,可以设置TabBarItem属性。如果视图控制器归uitabarviewcontroller所有,则选项卡栏上的关联项将相应更新。

    像这样的东西

    -(void)viewDidLoad {
        [super viewDidLoad];
        UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
        [self setTabBarItem:tbi]
        [tbi release];
    }
    

    显然,您不限于在viewdidload方法中执行此操作。

    推荐文章