ios 如何设置根视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8130218/
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
How to set root view controller
提问by pdenlinger
I set up an empty app with only an app delegate class, then subclassed a view controller class to create a xib to layout the app and make connections.
我设置了一个只有一个应用程序委托类的空应用程序,然后将一个视图控制器类子类化以创建一个 xib 来布局应用程序并建立连接。
But when I tried to run the app on iOS Simulator, I got an error which read: CoinToss[6212:f803] Applications are expected to have a root view controller at the end of application launch Terminating in response to SpringBoard's termination. Program ended with exit code: 0
但是当我尝试在 iOS 模拟器上运行该应用程序时,我收到一个错误消息:CoinToss[6212:f803] 应用程序预计在应用程序启动结束时有一个根视图控制器终止以响应 SpringBoard 的终止。程序以退出代码结束:0
What do I need to do in order to create a root view controller for the app?
我需要做什么才能为应用程序创建根视图控制器?
Thank you.
谢谢你。
回答by
in AppDelegate.m
在 AppDelegate.m 中
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:];
self.window.rootViewController = viewController;//making a view to root view
[self.window makeKeyAndVisible];
return YES;
}
回答by Caleb
Since you're apparently using .xib files, load your view controller and the set the window's rootViewController
property to your view controller in -application:didFinishLaunchingWithOptions:
.
由于您显然在使用 .xib 文件,因此加载您的视图控制器并将窗口的rootViewController
属性设置为 .xib 文件中的视图控制器-application:didFinishLaunchingWithOptions:
。
回答by Ahmed Z.
You need to set 2 things for that..
你需要为此设置两件事..
in AppDelegate.mfile:
_applicationDidFinishLaunchingWithOptions_
self.window.rootViewController = self.viewController;
in application.m
retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
在AppDelegate.m文件中:
_applicationDidFinishLaunchingWithOptions_
self.window.rootViewController = self.viewController;
在application.m
retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
回答by jeet.chanchawat
Applications are expected to have a root view controller
应用程序应该有一个根视图控制器
Replace in AppDelegate
在 AppDelegate 中替换
[window addSubview:[someController view]];
to
到
[self.window setRootViewController:someController];