0

I am new and am working on an exercise which involves starting with a navigation-based template. Since I am running Xcode 4.2 which no longer has that template, I have started with an empty application template, and then copied the directory structure of the completed app.

Since the empty app template starts only with an AppDelegate.h, .m files, I started adding other required files, including the MainWindow.xib, and the RootViewController.h, .m files. Did some tweaking of the #import directive so that it could see the right files, and could start alright.

However, when I try to run it on the iOS Simulator, I got this message: 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

What additional changes do I need to make so that the app can see the RootViewController? Thank you.

pdenlinger
  • 3,897
  • 10
  • 60
  • 92
  • Related sidetrack: Does anyone know why they removed that template from 4.2? – Luke Nov 15 '11 at 22:25
  • @Luke: To push the newfangled storyboards on everybody? – Emile Cormier Nov 15 '11 at 23:29
  • I would like to know if you have resolved this issue. If you have, please share the solution. Thanks! – yoninja Mar 16 '12 at 16:04
  • Hi yoninja-- Found out that you should start with a Navigation or Tab Controller first. Think of either of these as a container view controller; it's invisible, but it loads in any of the other view controllers. This will then load in the visible root controller, which will be the first screen, and depending on the user's actions, will load the other views and view controllers. – pdenlinger Mar 17 '12 at 17:24
  • A good source for information in explaining view controllers is Apple's documentation in Xcode. Just search on "view controllers" – pdenlinger Mar 17 '12 at 20:54
  • Possible duplicate of [Applications are expected to have a root view controller at the end of application launch](https://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati) – Cristik Feb 03 '19 at 09:04

5 Answers5

1

In -[AppDelegate application:didFinishLaunchingWithOptions:], you need to set your window's rootViewController property.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

You need to set the rootViewController property of AppDelegate's _window:

- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _window.rootViewController = self.myNavigationController;
    [_window makeKeyAndVisible];
    return YES;
}

To start a traditional navigation-based project in XCode 4.2, I find it easier to start with the single-view template. Then, in AppDelegate, I substitute the generated UIViewController with a UINavigationController.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
  • Does that mean I should delete the UIViewController files (.h, .m, .xib) files after I have created the UINavigationController files? – pdenlinger Nov 15 '11 at 22:55
  • You can delete them, or you can reuse them to be the top (root) view controller for your navigation controller. If you want to rename a view controller, it's best to right-click on the view controller's name in the source code, and do Refactor->Rename. – Emile Cormier Nov 15 '11 at 23:26
  • The completed app only has a RootViewController and TableViewController subclassed from it. Does that mean the RootViewController should play the same role as the NavigationController? – pdenlinger Nov 16 '11 at 00:58
0
self.window.rootViewController=self.yourviewControollerobj
Taryn
  • 242,637
  • 56
  • 362
  • 405
0

You can set like this. First set the window bounds then add you navigation controller with root view controller.

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

        MainViewController *vc = [MainViewController new];
        /*
        * If you are using .xib you should create UIViewontroller like this
        * MainViewController *vc = [MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]
        */

        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

        [self.window setRootViewController:self.nc];

        [self.window makeKeyAndVisible];

        return YES;
    }
Batu
  • 191
  • 1
  • 8
-2
 MyViewController *rootCtr = [[MyViewController alloc] init];

 [rootCtr.view addSubview:myView];

 window.rootViewController = rootCtr;
Marin
  • 12,531
  • 17
  • 56
  • 80