2011-12-19 63 views
0

重写RootViewController.xib在UISplitViewController安装在iPad上,RootViewControllerUIViewController类与XIB文件(不是UITableViewController在UISplitViewController

我的App有几个目标。根据所选的目标(并通过代码中的#ifdef ...),我想为RootViewController指定一个不同的XIB文件。

我想的变化具有application:didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Add the split view controller's view to the window and display. 
    self.window.rootViewController = self.splitViewController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

== EDIT ==

我删除从MainWindow.xib中所有控制器进行,然后加入在AppDelegate中以下行。 RootViewController与相应的XIB一起启动,但SplitViewController中的RootVC和DetailsVC之间的机制不起作用;即。当击中RootVC中的一个应该触发DefaultVC变化的按钮时,什么都不会发生。我明显错过了一些东西。

splitViewController = [[UISplitViewController alloc] init]; 

#ifdef OPTION1 
    rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_1" bundle:nil]; 
#elif OPTION2 
    rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_2" bundle:nil]; 
#endif 

defaultViewController = [[[DefaultViewController alloc] init] autorelease]; 

UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease]; 
UINavigationController *defaultNav = [[[UINavigationController alloc] initWithRootViewController:defaultViewController] autorelease]; 

splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, defaultNav, nil]; 
splitViewController.delegate = defaultViewController; 

self.window.rootViewController = self.splitViewController; 
[self.window makeKeyAndVisible]; 

// Add the split view controller's view to the window and display. 
self.window.rootViewController = self.splitViewController; 
[self.window makeKeyAndVisible]; 

回答

1

您的代码示例似乎是一个很艰难和困惑的方式做一个拆分视图控制器,所以我就满足了更普遍的问题:

我的App有几个目标。根据所选的目标(并通过代码中的#ifdef ...),我想为RootViewController指定一个不同的XIB文件。

如果您在项目中定义了多个目标,则不需要#ifdef即可完成此操作。使用构建过程的目标机制要简单得多。

假设您有两个资源,都称为“MyFunResource”,但您希望在名为“blue”的目标中使用一个资源,而您希望在另一个名为“red”的目标中使用另一个资源。

将这两种资源添加到您的项目中。但在文件检查器(“视图”>“实用程序”>“显示文件检查器”)中,请注意“目标成员”部分。请注意,您的所有目标都在其旁边的复选框中列出。在构建给定目标时,只有在此处选中了目标名称时,才会将所选资源复制到该束中。

因此,选择您想要在“红色”目标中使用的“MyFunResource”,并确保“红色”是“目标成员资格”中唯一带有复选标记的选项。然后选择一个你想要的“蓝色”,并确保只有“蓝色”被选中。

现在,当您构建红色目标时,构建系统将只将与红色相关的资源复制到该包中,这样神奇地,红色资源就是在运行时使用的资源。反之亦然,蓝色。没有代码或'#ifdef的必需。

+0

谢谢杰姆斯,这绝对是比我想出来的更好的方式。 – 2012-07-01 14:32:53

+1

我希望Apple会发布一些关于项目管理的好文档(甚至是WWDC视频!)。我每天都要通过试验和错误来发现太多东西 - 其中大部分都是在NeXTSTEP的旧版Project Builder中发现的! – jemmons 2012-07-01 14:37:52

0

您可以使用此

#ifdef Target1 
self.window.rootViewController = [[RootViewController alloc] 
      initWithNibName:@"RootViewControllerTarget1" bundle:nil]; 
#ifdef Target2 
self.window.rootViewController = [[RootViewController alloc] 
       initWithNibName:@"RootViewControllerTarget2" bundle:nil]; 
// etc... 

initWithNibName使用指定的.xib文件中的控制器。 UIViewController initWithNibName:bundle reference

+0

感谢iska,但这不起作用,因为rootViewController会占用所有的屏幕。 'splitViewController。viewControllers'预计有一个数组,这里有2个控制器; 'rootViewController'和''defaultViewController'“ – 2011-12-20 07:09:24

+0

请参阅我编辑的带2个控制器的版本。 – 2011-12-20 07:18:27

+0

@Adriano您是否检查过两个.xib文件中的所有插座和连接?另一件事,它是否适用于一个.xib,但不适用于另一个?或者它们都失败了? – iska 2011-12-20 15:00:21

0

我从来没有找到如何通过XIB做到这一点。我最终以编程方式完成了它;我使用一个XIB并根据目标进行填充。