ios 应用程序窗口应该在应用程序启动结束时有一个根视图控制器 - 即使所有已知问题都已修复
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12784411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application windows are expected to have a root view controller at the end of application launch - even with all known issues fixed
提问by Thomas Clayson
I have this problem, however none of the information I can find on this forum or the internet in general seems to be able to help me.
我有这个问题,但是我在这个论坛或互联网上找到的所有信息似乎都无法帮助我。
There seem to be two places where this error can come about:
似乎有两个地方可能会出现此错误:
- main.m - my function looks like this:
- main.m - 我的函数如下所示:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
The last argument in UIApplicationMain
returns an NSString
value of the class of my AppDelegate
. This is therefore working fine.
中的最后一个参数UIApplicationMain
返回NSString
my 的类的值AppDelegate
。因此,这工作正常。
2.AppDelegate.m - there is an "older" way of setting the root view controller which is like this:
2.AppDelegate.m - 有一种设置根视图控制器的“旧”方法,如下所示:
[self.window addSubview:rootViewController];
However, in my app it has already been updated to:
但是,在我的应用程序中,它已更新为:
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
So none of the current information on the internet works. It is slightly more puzzling as my colleague can get it to work on his computer perfectly fine - he was the one that sent me the app source code so all the settings and code should be exactly the same.
因此,互联网上的当前信息均无效。这有点令人费解,因为我的同事可以让它在他的计算机上完美运行 - 他是向我发送应用程序源代码的人,因此所有设置和代码都应该完全相同。
I am trying to launch this in the simulator. It is built against iOS 5, but I'm trying to run it on the iOS 6.0 simulator.
我正在尝试在模拟器中启动它。它是针对 iOS 5 构建的,但我正在尝试在 iOS 6.0 模拟器上运行它。
I have the latest XCode (4.5.1).
我有最新的 XCode (4.5.1)。
Is there any reason this would be happening? And how can I rectify it?
有什么理由会发生这种情况吗?我该如何纠正它?
Many thanks
非常感谢
Tom
汤姆
回答by ordinaryaverageguy
I ran into exactly the same thing trying to add a UITableView to a single-view app. Instead, create a default Master-Detail Application project (file->new->target->...) and see the AppDelegate's implementation of didFinishLaunchingWithOptions:
我在尝试将 UITableView 添加到单视图应用程序时遇到了完全相同的事情。相反,创建一个默认的主从应用项目(文件->新建->目标->...)并查看 AppDelegate 对 didFinishLaunchingWithOptions 的实现:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MDMasterViewController *masterViewController = [[MDMasterViewController alloc] initWithNibName:@"MDMasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Rather than directly setting your view controller as the window's rootViewController, you need to create a navigation controller init'ed with your view controller for initWithRootViewController, then set that nav controller as the window's rootViewController. (Notice you also have to squirrel away that nav controller in a property so it doesn't get destructed).
而不是直接将您的视图控制器设置为窗口的 rootViewController,您需要创建一个导航控制器,使用您的视图控制器为 initWithRootViewController 初始化,然后将该导航控制器设置为窗口的 rootViewController。(请注意,您还必须将那个导航控制器放在一个属性中,这样它就不会被破坏)。
回答by Andrew Smith
Just change this:
只需改变这个:
[window addSubview:tabBarController.view];
to this:
对此:
[window setRootViewController:tabBarController];
Or whatever was in the addSubView:
或者 addSubView 中的任何内容:
回答by Mateus
Try to define the default view controller in your project menu,
尝试在项目菜单中定义默认视图控制器,
select your project => Summary => Main Interface => Type your main view controller
选择你的项目 => 摘要 => 主界面 => 输入你的主视图控制器
every time that i started new project i faced the same error as you,doing this every time solved,hope this help you.
每次我开始新项目时,我都遇到和你一样的错误,每次都解决这个问题,希望这对你有帮助。
回答by Rui Peres
Do the following add an Exception BreakPoint on your code at least to see if you can figure out what's the problem. Use thistutorial if you don't know how to do it. Also, are there any warnings when you compile? Even if it let's you compile, sometimes warnings can reveal why your app is crashing.
执行以下操作,至少在您的代码上添加一个 Exception BreakPoint 以查看您是否能找出问题所在。如果您不知道如何操作,请使用本教程。另外,编译时是否有任何警告?即使它让您编译,有时警告也可以揭示您的应用程序崩溃的原因。