在应用程序打开时加载所有Viewcontrollers

时间:2013-11-06 08:31:11

标签: ios objective-c xcode uitabbarcontroller

我正在开发iOS的标签式应用程序。因为tabbar图标分别加载到每个viewcontroller中,我想知道是否可以在后台一次加载所有viewcontrollers,以便在启动应用程序时加载所有tabbar图标。

tabbar为每个tabbar项目(选中和未选中)使用两个图标,这就是为什么我选择单独加载每个viewcontroller中的图标

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

3 个答案:

答案 0 :(得分: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"]];

答案 1 :(得分:0)

对您的UITabBarController进行子类,并在viewWillAppear上设置图片。我发现这是最干净的方式。

答案 2 :(得分:0)

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

- (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对象时,将设置tabbar图标。

viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];