0

I'm trying to add a UINavigationController to my ViewController. And when I launch the app, it gives me just a black screen and dont init the app.

This is my AppDelegate.m :

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

UIViewController *rootView = [[ViewController alloc]
                              initWithNibName:@"ViewController"
                              bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:rootView];
[[self window] setRootViewController:self.navController];

//template code
[self.window makeKeyAndVisible];
[rootView release];
return YES;

}

I'm following this article: http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/

What's wrong? Thanks!

7
  • Shouldn't you have "ViewController *rootView" instead of "UIViewController *rootView"? It looks right to me though. Having said that, I'd suggest using ARC and storyboards to make everything a bit easier. Commented May 15, 2013 at 17:02
  • Yes, looks fine to me too. About ARC and storyboards, I dont have experience with that yet. Commented May 15, 2013 at 17:07
  • Aparently your ViewController class is named "ViewController". I guess it is a subview of UIViewControlelr and has a file ViewController.xib in which you design the actual view in interface builder? Commented May 15, 2013 at 17:08
  • @Fogmeister, actually UIViewController* would do nicely here as he does not access any method or property that is unique to his subclass ViewController. I, too, don't see a problem in this code. How about nib file (xib extension)? What did you do there? Commented May 15, 2013 at 17:10
  • 1
    Don't be afraid of ARC. It just makes (most) things easier. And don't be afraid of storyboard. However, I'd suggest dong your very first app, or the second, programmatically only. Code to push a second view controller and present a third one modally. Then do the same in IB/Storyboard and you will understand from the beginning how things are linked and work together. Plus you understand how IB and storyboard help getting efficient. But without knowing how it would work programatically you will waist more time searching for errors than you actually save by using a storyboard. Commented May 15, 2013 at 17:16

2 Answers 2

1

Try to use ARC adn storyboard If you are targeting IOS 5+, it would be a lot easier and you wouldn even have to code, just drag and drop the navigation controller to storyboard.

havent test but try this:

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

UIViewController *rootView = [[ViewController alloc]
                              initWithNibName:@"ViewController"
                              bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:rootView];
        self.window.rootViewController =nil;
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
return YES;

}
Sign up to request clarification or add additional context in comments.

2 Comments

Nothing. Still giving me black screen.
are you sure your nibname is correctly set to ViewController in interfacebuilder? also I would suggest you this tut : raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
0

Just testing with a project and used this code...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[CCViewController alloc] initWithNibName:@"CCViewController" bundle:nil];
    UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navControl;
    [self.window makeKeyAndVisible];
    return YES;
}

Worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.