仅在iPad上"Application windows are expected to have a root view controller at the end of application l



我正在尝试将我的iPhone仅应用程序转换为通用应用程序。我将设备切换到Universal,让Xcode完成制作MainWindow-iPad的工作。xib对我来说,现在当我在iPhone模拟器中运行应用程序时,它工作得很好,但当我在iPad模拟器中运行它时,我得到一个白屏和Application windows are expected to have a root view controller at the end of application launch错误。我也读过其他一些关于同样问题的帖子,但没有一个局限于一个设备。

这是我的application:didFinishLaunchWithOptions:方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 /* some dropbox setup stuff */

// INIT VIEW AND CORE DATA
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
    // Handle the error.
}
rootViewController.managedObjectContext = context;
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[_window addSubview:[_navigationController view]];
[_window makeKeyAndVisible];
[rootViewController release];
[aNavigationController release];
return YES;
}

编辑:我只有一个根视图控制器是iPhone的大小称为RootViewController。但它还是应该载入的,不是吗?如果不需要,如何为iPad创建一个呢?

更改以下行:

[_window addSubview:[_navigationController view]];

:

_window.rootViewController = _navigationController;

或者,如果你需要兼容iOS 3:

if ([_window respondsToSelector:@selector(setRootViewController:)]) {
    _window.rootViewController = _navigationController;
} else {
    [_window addSubview:_navigationController.view];
}

你需要用iPad的xib文件创建一个RootViewController,否则你会得到这个错误。下面是Xcode为通用应用程序提供的模板代码。如果你在iPad模拟器中调试应用程序,并指向调试器运行创建视图控制器与iPhone xib文件,你会看到确切的错误。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[SYKViewController alloc] initWithNibName:@"SYKViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[SYKViewController alloc] initWithNibName:@"SYKViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

在ios4及以后的版本中,UIWindow具有可设置的属性rootViewController。这是推送应用程序启动时显示的UIViewControllerUINavigationController。在Xcode IB中,选择Initial Scene: UINavigationController的初始视图控制器设置一切,不需要代码。

从生成的MainWindow-iPad。xib,在Interface Builder中,在IB中添加一个视图控制器对象,以及视图下面的对象。对于对象,将它的类设置为AppDelegate,对于视图控制器,将类设置为ViewController(在身份检查器中)并在属性检查器中指定nib名称。你可以看看主窗口。您正在转换的设备的笔尖,以查看差异

编辑:我忘了提到一些重要的步骤。你还需要在IB中将File的Owner类设置为"UIApplication",并为App Delegate和View Controller设置适当的引用出口。同样,在IB中,最简单的方法是查看主窗口笔头的连接检查器并模拟它。如果你有另一个特定于iPad的笔尖,除了MainWindow-iPad。nib(即ViewController-iPad.nib),一定要选择它的文件的所有者,并把它指向视图,并适当地设置它的类。

我试了你的建议,但没有一个对我有效,对不起。:/我最终只是在没有接口构建器的代码中手动创建视图:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    // Setup view for iPad
} else
    // Setup view for iPhone and iPod Touch
}

我原以为会比实际困难得多。

注意,如果你要在iPhone和iPad上使用相同的对象,你仍然可以在interface builder中连接所有的东西并改变这些块中对象的框架。

相关内容

最新更新