Why doesn’t iOS 5.0 like plain window applications? Why does it request that view controllers be used?

你。 提交于 2019-12-05 00:11:30


I have an iOS app that I created with Xcode 4.0’s “Window-based Application” template. It worked fine back then and it was using the iOS 4.3 SDK. This is an app that simply puts the buttons, labels, etc. directly onto a window. No view controllers—no nothing.

But now that I’ve upgraded to Xcode 4.2 (and its iOS 5.0 SDK), and I run the app, this message gets logged to the console when the app launches in the simulator:

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

To be sure, the app continues to work, but this rather bothersome log gets printed out on every launch.

Why is this happening? Why does iOS 5.0 prefer/request view controllers?


I don't know specifically why the message is logged, but integration between UIWindow and UIViewController has been increasing over the last several iterations of iOS. iOS 4 added a rootViewController property to UIWindow. The two classes work together to manage view rotation. Given the new capabilities that iOS 5 introduced to UIViewController (specifically, the ability to create your own container view controllers), it's clear that the relationship between the two classes will continue to evolve. As you've said, your app continues to function in iOS 5, so having a root view controller isn't a hard and fast requirement yet. Perhaps there are features planned for future iOS versions that will depend on having a view controller available.

I don’t have anything against them, and I will use them if iOS wants me to. I was just curious about the above behavior.

I'd interpret the logged message as a gentle but persistent nudge from Apple toward providing a root view controller. Most apps already use view controllers anyway, so this isn't a big change, but there are probably a number of apps out there that don't properly set the window's rootViewController property to their top-level view controller.


You have just Connected your "view" with "File Owner".....just remove that connection and run your app.click your view and see the connections inspector area and remove that connection which I told..I dont know exactly what reason..but i had this issue and i cleared. May be you have used some tableview or some other views in it.So the app. needs a view controller to launch.If you remove that connection then it will run what you wrote in a code only...


Let me tell you the reason.

In former edition, Window-based Application is used to build multi-view applications.But in Xcode 4.2,the Window-based Application does not exist any more. Empty Application is designed to establish a multi-view program.

The difference between Window-based Application and Empty Application is that the former has a main window, the MainWindow.xib.

When the program starts, iPhone build the main window firstly. The content in the MainWindow will be loaded and built.But if you want more functions, e.g. multi-view, you still have to new a root view controller. Root view controller can help manage the views in your program. In the new edition,Xcode 4.2, a root view controller is expected.

In the new edition, there is not a MainWindow.xib in Empty Application.The AppDelegate create a window instead. And it wants a root view controller. So the best way is to create a UIViewController Subclass with XIB for interface for MainWindow. But in the old edition, XIB is not needed.

So get it? Without a root view controller, you are not going to receive an error, but you can hardly do any thing without one. That's why the warning always come out.