1

我正在为 iOS 开发一个选项卡式应用程序。因为标签栏图标分别加载到每个视图控制器中,所以我想知道是否可以在后台一次加载所有视图控制器,以便在启动应用程序时加载所有标签栏图标。

标签栏为每个标签栏项目使用两个图标(选中和未选中),这就是为什么我选择在每个视图控制器中分别加载图标

否则,是否有可能在 App 委托中加载标签栏图标?

4

3 回答 3

1

是的,有可能。您可以在 App 委托的 didFinishLaunchingWithOptions 方法中初始化所有控制器和所有图像。这是示例:

    UIViewController *locateTabController = [[LocationTabController alloc] initWithNibName:@"LocationTabController" bundle:nil];

    UINavigationController *locationTabNavigationController = [[UINavigationController alloc] initWithRootViewController:locateTabController];

    // Product Tab
    UIViewController *productsTabController = [[ProductsTabController alloc] initWithNibName:@"ProductsTabController" bundle:nil];
    UINavigationController *productsTabNavigationController = [[UINavigationController alloc] initWithRootViewController:productsTabController];

    // Delivery Tab
    UIViewController *nextDeliveryTabController = [[DeliveryTabController alloc] initWithNibName:@"DeliveryTabController" bundle:nil];
    UINavigationController *nextDeliveryTabNavigationController = [[UINavigationController alloc] initWithRootViewController:nextDeliveryTabController];

    //  Order Tab
    UIViewController *standingOrderTabController = [[OrderTabController alloc] initWithNibName:@"OrderTabController" bundle:nil];
    UINavigationController *standingOrderTabNavigationController = [[UINavigationController alloc] initWithRootViewController:standingOrderTabController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationTabNavigationController, productsTabNavigationController,nextDeliveryTabNavigationController, standingOrderTabNavigationController, nil];

这里提供了导航控制器,因为每个选项卡控制器类都有自己的导航。

您可以同时添加标题和图像。

/// 在每个标签栏控制器上添加标题

[[self.tabBarController.viewControllers objectAtIndex:0] setTitle:@"Locate"];
[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"LocateIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"LocateIconInactive.png"]];

[[self.tabBarController.viewControllers objectAtIndex:1] setTitle:@"Products"];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"ProductsIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ProductsIconInactive.png"]];

[[self.tabBarController.viewControllers objectAtIndex:2] setTitle:@"Delivery"];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"NextDeliveryIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"NextDeliveryIconInactive.png"]];

[[self.tabBarController.viewControllers objectAtIndex:3] setTitle:@"Order"];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"StandingOrderIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"StandingOrderIconInactive.png"]];
于 2013-11-06T08:56:51.527 回答
0

对您的子类进行分类UITabBarController并将图像设置在viewWillAppear. 我发现这是最干净的方法。

于 2013-11-06T08:45:26.180 回答
0

为此目的打开应用程序时,您不需要加载任何视图控制器。您所要做的就是在每个视图控制器的初始化方法中设置标签栏图标,如下所示:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        self.tabBarItem.image = [UIImage imageNamed:IMAGE_NAME];
        [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:IMAGE_NAME] withFinishedUnselectedImage:[UIImage imageNamed:IMAGE_NAME]];
        self.tabBarItem.title = TITLE;
    }
    return self;
}

之后,当您从 AppDelegate 初始化 viewcontroller 对象时,将设置标签栏图标。

viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
于 2013-11-06T08:45:53.210 回答