如何将标签栏控制器添加到拆分视图控制器应用程序的详细视图中

时间:2014-04-23 14:42:48

标签: objective-c ios7 xcode5

我正在为iPad / iPhone设备制作目录应用程序。我在XCode中使用主视图模板有一个基础,在根或主导航控制器中我显示了产品列表,现在我试图添加一个标签控制器细节视图,因为每个产品有3个不同的部分,一个有一些细节,另一个有规格表,另一个有产品的图片或图像。

我以编程方式执行此操作,这是我的AppDelegate.h代码

    #import <UIKit/UIKit.h>

    #import "MVADetailVwCtrl.h"
    #import "MVATableVwCtrl.h"
    #import "MVAModelVwCtrl.h"

    @interface MVAAppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) UITabBarController *rootTabBarCtrl;
    @property (strong, nonatomic) MVADetailVwCtrl *detailVwCtrl;
    @property (strong, nonatomic) MVATableVwCtrl *tableVwCtrl;
    @property (strong, nonatomic) MVAModelVwCtrl *modelVwCtrl;
    @property (strong, nonatomic) NSArray* viewControllers;

    @end

这是AppDelegate.m上的代码

    #import "MVAAppDelegate.h"

    @implementation MVAAppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

        self.detailVwCtrl = [[MVADetailVwCtrl alloc] init];
        // self.newMoveVwCtrl.title = @"Product details";
        UINavigationController *detailNavCtrl = [[UINavigationController alloc] initWithRootViewController:self.detailVwCtrl];

        self.tableVwCtrl = [[MVATableVwCtrl alloc] init];
        // self.myAccountsVwCtrl.title = @"Table";
        UINavigationController *tableNavCtrl = [[UINavigationController alloc] initWithRootViewController:self.tableVwCtrl];

        self.modelVwCtrl = [[MVAModelVwCtrl alloc] init];
        // self.settingsVwCtrl.title = @"Pictures";
        UINavigationController *modelNavCtrl = [[UINavigationController alloc] initWithRootViewController:self.modelVwCtrl];

        self.viewControllers = [NSArray arrayWithObjects:
                                detailNavCtrl,
                                tableNavCtrl,
                                modelNavCtrl,
                                nil];

        self.rootTabBarCtrl = [[UITabBarController alloc] init];
        self.rootTabBarCtrl.viewControllers = self.viewControllers;

        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
            UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
            splitViewController.delegate = (id)navigationController.topViewController;
        }

        UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
        splitViewController.viewControllers = [NSArray arrayWithObjects: self.rootTabBarCtrl, [splitViewController.viewControllers lastObject], nil];

        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

        [[UINavigationBar appearance] setBarTintColor:UIColorFromHex(0x009B3E)];

        return YES;
    }

我创建每个选项卡和每个选项卡的每个导航控制器并将它们全部存储在一个数组中,然后我检索拆分视图的视图控制器以使用新选项更改它们但是我不知道这是否是最接近的。

也许有一种编辑故事板的最佳方法,我希望保持该应用程序与iPad和iPhone设备的兼容性。

你能帮帮我吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

只需在拆分视图中切换视图控制器的顺序,这将切换详细信息和主视图。

  splitViewController.viewControllers = [NSArray arrayWithObjects: [splitViewController.viewControllers firstObject], self.rootTabBarCtrl, nil];`

答案 1 :(得分:0)

您发布的代码无法在iPhone上运行,因为只有iPad支持UISplitViewController。 您是否考虑过使用Storyboard而不是以编程方式执行此操作?我认为在你的情况下,这可以使事情变得容易多了。在这篇Stanford course中,老师解释了如何使用storyboard universal制作iPhone应用程序,他还使用了分割视图控制器。

相关问题