0

我对自定义 UITabBarItem 有疑问

首先可以下载我的代码demo

现在,我在 MyAppDelegate 的自定义 UITabbarcontroller 是:

-(void)configureiPhoneTabBar
{
    tabViewController  = (UITabBarController *)self.window.rootViewController;
    UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0];
    [self configureTabBarItemWithImageName:@"home_ON.png" : @"home.png" andText:@"Home" forViewController:controller1];

    UIViewController *controller2 = [[tabViewController viewControllers] objectAtIndex:1];
    [self configureTabBarItemWithImageName:@"tvChannel_ON.png" : @"tvChannel.png" andText:@"TV" forViewController:controller2];
}

-(void)configureTabBarItemWithImageName:(NSString*)imageName1 : (NSString*)imageName2 andText:(NSString *)itemText forViewController:(UIViewController *)viewController
{
    UIImage* icon1 = [UIImage imageNamed:imageName1];
    UIImage* icon2 = [UIImage imageNamed:imageName2];

    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:itemText image:icon1 tag:0];
    [item1 setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] }
                         forState:UIControlStateNormal];
    [item1 setFinishedSelectedImage:icon1 withFinishedUnselectedImage:icon2];
    [viewController setTabBarItem:item1];
}

我在 UINavigationController 中使用 UITabbarcontroller,我无法从 appdelegate 自定义 uitabaritem,如果您运行代码并检查 UITabBarController 中的“初始”,界面显示为真,但是当在 UINavigationController 中检查“初始”时很难这样做

4

2 回答 2

0

如果您从登录视图控制器启动您的应用程序,您可以从该初始视图控制器配置 tabBarController。您可以在 a 中执行此操作prepareForSegue,因为在那里您将有一个指向 tabBarController 的指针segue.destinationViewController...

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        [self configureiPhoneTabBar:segue.destinationViewController];
    }

将您的 tabBarViewController 配置代码移动到此视图控制器中...

 -(void)configureiPhoneTabBar:(UITabBarController*)tabViewController
 {
        // tabViewController  = (UITabBarController *)self.window.rootViewController;
    UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0];
        //...etc...

或发送configureiPhoneTabBar:给您的应用委托进行配置(但实际上最好将代码保留在您的应用委托之外)。

更好的是,您可以继承 UITabBarViewController 并将您的配置代码放在那里,由 viewDidLoad 触发:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureiPhoneTabBar];
}


-(void)configureiPhoneTabBar
  {
    UIViewController *controller1 = [[self viewControllers] objectAtIndex:0];

        ...etc...

作为另一种选择,您可以在加载到选项卡栏的相应视图控制器中找到配置代码。

于 2013-01-25T05:35:18.377 回答
0

将自定义标签栏的代码移动到 LoginViewController (这是与标签栏控制器分离的那个)。我将此代码添加到该文件的末尾:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"idenLogin"]) {
        UITabBarController *tabViewController  = segue.destinationViewController;
        UIViewController *controller1 = [[tabViewController viewControllers] objectAtIndex:0];
        [self configureTabBarItemWithImageName:@"home_ON.png" : @"home.png" andText:@"Home" forViewController:controller1];
        UIViewController *controller2 = [[tabViewController viewControllers] objectAtIndex:1];
        [self configureTabBarItemWithImageName:@"tvChannel_ON.png" : @"tvChannel.png" andText:@"TV" forViewController:controller2];
    }
}


-(void)configureTabBarItemWithImageName:(NSString*)imageName1 : (NSString*)imageName2 andText:(NSString *)itemText forViewController:(UIViewController *)viewController {
    UIImage* icon1 = [UIImage imageNamed:imageName1];
    UIImage* icon2 = [UIImage imageNamed:imageName2];

    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:itemText image:icon1 tag:0];
    [item1 setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor] }
                         forState:UIControlStateNormal];
    [item1 setFinishedSelectedImage:icon1 withFinishedUnselectedImage:icon2];
    [viewController setTabBarItem:item1];
}
于 2013-01-25T07:24:36.030 回答