简体   繁体   English

在制作没有ViewController的应用程序时如何使用RootViewController?

[英]How do I use a RootViewController when making an app without a ViewController?

I am trying to make a simple app from a tutorial that does not have a viewController at all. 我正在尝试从一个根本没有viewController的教程制作一个简单的应用程序。 All the code is in the AppDelegate. 所有代码都在AppDelegate中。 I am on xcode 4.2 and I am getting this error: 我在xcode 4.2上,出现此错误:

Applications are expected to have a root view controller at the end of application launch

I'm not sure how to deal with this. 我不确定如何处理。 There are some blogs out there with fixes but none of them are working for me and I really would like to understand what is going on here. 那里有一些修正的博客,但是没有一个对我有用,我真的很想了解这里发生了什么。 And, how to fix it. 并且,如何解决它。

I do have a view that contains some buttons and labels. 我确实有一个包含一些按钮和标签的视图。 But I have no "ViewController". 但是我没有“ ViewController”。 The files contained in my project are: AppDelegate.h, AppDelegate.m, and Window.xib only. 我的项目中包含的文件只有:AppDelegate.h,AppDelegate.m和Window.xib。 There is no ViewController.h, ViewController.m 没有ViewController.h,ViewController.m

** edit ** **编辑**

I ended up making the app from a 'view based application' instead and just moving all the logic from the app delegate to the view controller. 我最终从“基于视图的应用程序”制作了该应用程序,然后将所有逻辑从应用程序委托移至了视图控制器。 So, I didn't really solve the problem per se. 因此,我本身并没有真正解决问题。 The app works now though. 该应用程序现在可以运行了。 Thanks for the help 谢谢您的帮助

It's not possible to have an iOS app that doesn't have a view controller. 不可能有没有视图控制器的iOS应用。 You can always create a trivial view controller, ie, 您始终可以创建一个简单的视图控制器,即

[[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds].rootViewController =
  [[[UIViewController alloc] init] autorelease];

While I agree that there may be workarounds, another question to address is: why do you want an app without a view? 虽然我同意可能有解决方法,但要解决的另一个问题是:为什么一个没有视图的应用程序? Even if it's designed to run in the background and present no visual interface, at least make a simple view showing the application name and version, a largeish icon and perhaps a status. 即使将其设计为在后台运行并且不显示任何视觉界面,至少也要进行一个简单的视图以显示应用程序名称和版本,一个大图标以及一个状态。 This kind of idle screen uses very little system resources, especially when the app is backgrounded, but improves the overall experience of the app. 这种待机屏幕使用的系统资源非常少,尤其是在后台运行应用程序时,但是可以改善应用程序的整体体验。

It sounds like you're looking at an old tutorial. 听起来您正在看旧的教程。 UIWindow got a rooViewController property in iOS4. UIWindow在iOS4中具有rooViewController属性。 I believe it became required in iOS5 to help keep controller hierarchies and view hierarchies in sync with the addition of custom container controllers (and to fix a corner case where replacing the "root controller" of a UIWindow could stop orientation changes from propagating). 我相信,在iOS5中它成为必需的,以帮助保持控制器层次结构和视图层次结构与自定义容器控制器的同步(并解决一个极端的情况,即替换UIWindow的“根控制器”可能会阻止方向更改的传播)。 There was a WWDC presentation in 2011 that explained this in some detail. WWDC在2011年发表了演讲,对此做了详细解释。 I think it was Session 102, Implementing UIViewController Containment . 我认为这是会议102, 实现UIViewController Containment

At then end of the day, there's no good reason not to have a root view controller. 在一天结束时,没有充分的理由不使用根视图控制器。 Apple wants to be able to assume it's there in their APIs going forward. 苹果希望能够假设其API一直存在。 If the tutorial you're looking at doesn't account for that, it's broken. 如果您正在查看的教程不能解决问题,则说明它已损坏。

If you set your deployment target to 4.3 and run on the iPhone 4.3 simulator, you won't get the warning. 如果将部署目标设置为4.3并在iPhone 4.3模拟器上运行,则不会收到警告。

To install the iOS 4.3 simulator, go to Xcode > Preferences > Downloads. 要安装iOS 4.3模拟器,请转到Xcode>偏好设置>下载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当infoButton位于RootViewController中时,如何从MainViewController中隐藏实用程序应用程序中的infoButton? - How do I hide an infoButton in a utility app from the MainViewController when the infoButton is in the RootViewController? 仅在关闭当前ViewController时显示RootViewController - Display RootViewController only when dismiss current ViewController 如何在iOS中从rootViewController推送Viewcontroller - How to push Viewcontroller from rootViewController in iOS 如何使上次访问的Viewcontroller成为RootViewController? - How To Make Last Visited Viewcontroller The RootViewController? 如何为每个标签使用使用相同ViewController的UITabBarController? - How do I use a UITabBarController using the same ViewController for every tab? 如何在实例化的Storyboard中将数据传递到UINavigationController的rootViewController? - How do I pass data to a rootViewController of a UINavigationController in an instantiated Storyboard? 从ViewController重新定义rootViewController - Redefining rootViewController from a ViewController 我可以将rootViewController更改为ViewController中的另一个Storyboard视图吗? - Can I change the rootViewController to a different storyboard view inside ViewController? 为什么我似乎必须在iOS应用程序委托中强制转换window.rootViewController? - Why do I seemingly have to typecast window.rootViewController in iOS app delegate? 如何将对象从一个ViewController传递到另一个ViewController - How do I pass object from one ViewController to another ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM