2010-08-14 45 views
6

我无法让UISplitViewController在通用应用程序中工作,我已经编写了iPhone部分。作为一种排除故障的方法,我决定从一个新项目开始,试着去做导致问题的一个动作,而且现在依然如此。在通用应用程序中不能使用UISplitViewController吗?

如果我创建一个通用应用程序,并在iPad控制器中创建一个拆分视图(在XIB或代码中),那么它显示为黑色(除非我设置背景色)。如果我在仅限iPad的应用程序中执行此操作,则显示效果良好。

如果有人可以自己测试一下,看看他们是否得到同样的东西,或者告诉我我哪里出错了,我会很感激。

  1. 在Xcode中,创建一个通用的“基于窗口”的应用程序。
  2. 进入iPad控制器并粘贴底部的代码。

我得到的是一个黑屏,而不是分割视图。相同的代码适用于仅iPad的项目。我做错了什么,或者什么配置错了?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    UISplitViewController *split = [[UISplitViewController alloc] initWithNibName:nil bundle:nil]; 

    UIViewController *vc1 = [[UIViewController alloc] initWithNibName:nil bundle:nil]; 
    vc1.view.backgroundColor = [UIColor redColor]; 

    UIViewController *vc2 = [[UIViewController alloc] initWithNibName:nil bundle:nil]; 
    vc2.view.backgroundColor = [UIColor blueColor]; 

    split.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil]; 

    [window addSubview:split.view]; 
    [window makeKeyAndVisible]; 

    [vc1 release]; 
    [vc2 release]; 
    [split release]; 

    return YES; 
} 

回答

3

首先,您不应该在didFinishLaunchingWithOptions中释放您的拆分视图。将它添加到你的界面(在UIWindow下)并且只在dealloc上释放它。其次,子类UISplitViewController如下:

@interface MySplitViewController : UISplitViewController 
{ 
} 
@end 
@implementation MySplitViewController 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 
@end 

三,你的didFinishLaunchingWithOptions应该是这样的:

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

    split = [[MySplitViewController alloc] init]; 

    UIViewController *vc1 = [[UIViewController alloc] init]; 
    vc1.view.backgroundColor = [UIColor redColor]; 

    UIViewController *vc2 = [[UIViewController alloc] init]; 
    vc2.view.backgroundColor = [UIColor blueColor]; 

    split.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil]; 

    [window addSubview:split.view]; 
    [window makeKeyAndVisible]; 

    [vc1 release]; 
    [vc2 release]; 

    return YES; 
} 
+0

你是对内存管理和附加伊娃。 shouldRotateToInterfaceOrientation:覆盖听起来不错,但它不适用于我。你有没有尝试过? – tonklon 2010-08-17 11:17:47

+0

如果子类化UISplitViewController不适合你,请尝试子类化每个UIViewController并重写shouldRotateToInterfaceOrientation:在每一个中。这可能是最好的方法...... – ian 2010-08-18 08:12:49

+0

这是splitview subcontrollers的autorotate和release问题的结合。 – codepoet 2010-08-20 04:51:13

相关问题