如何在tabBarController中使用两个相同的ViewController.xib

时间:2012-04-10 18:20:38

标签: iphone objective-c ios xcode ipad

我使用tabBarController来显示照片,并在每个标签中显示每种照片,  所以我使用一个ViewController.xib,以及如何在每个标签中显示不同的内容(Navigation Item和ImageView)?

我的问题是: 下一步在哪里以及如何编写代码,代码在 - (void)tabBarController或PhotoController.m中? -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *photoController1 = [[[PhotoController alloc] initWithNibName:@"PhotoController" bundle:nil] autorelease];
UIViewController *photoController2 = [[[PhotoController alloc] initWithNibName:@"PhotoController" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: 
                                         ,photoController1
                                         ,photoController2
                                         ,nil];
self.tabBarController.delegate=self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{       
    switch (tabBarController.selectedIndex) 
{
    case 4:     
        //how to write code;
        break;
    case 5:
        //how to write code;
    default:
        break;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在PhotoController中制作一些准备方法,并将其公开,这样您就可以从tabBarController didSelectViewController中调用它,所以代码可能如下所示:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {       
    switch (tabBarController.selectedIndex)  {
    case 4:     
            //configure options for photoController
            [viewController prepareForDisplayWithOptions:options];
        break;
    case 5:
            //configure options for photoController2
            [viewController prepareForDisplayWithOptions:options2];
        break;
    default:
        break;
    } 

}