Question

Undoubtedly another silly question, as, once again, I'm missing something fundamental no doubt.

I have the following in my header file:

#import <UIKit/UIKit.h>
#import "CRPUserNameViewController.h"
#import "CRPLegalViewController.h"
#import "CRPMainInformationViewController.h"

@interface CRPInitialOpenViewController : UIViewController

@property (strong, nonatomic) CRPUserNameViewController *vcUsername;
@property (strong, nonatomic) CRPLegalViewController *vcLegal;
@property (strong, nonatomic) CRPMainInformationViewController *vcMain;
@property (strong, nonatomic) UINavigationController *ncMain;

@end

I'm simply trying to get a Navigation Controller to load and become the rootViewController replacing the current UIViewController with the following code:

self.view.backgroundColor = [UIColor blueColor];

_vcMain = [[CRPMainInformationViewController alloc] initWithNibName:nil bundle:nil];
_ncMain = [[UINavigationController alloc] initWithRootViewController:_vcMain]

self.view.window.rootViewController = _ncMain;
[self.view.window addSubview:_ncMain.view];

Shouldn't be that hard!! It seemed to work before! Even if I add:

[self.view removeFromSuperview];

I cannot see the Navigation Controller. I'm adding the blue background just to make sure if it is still there!

What am I missing as once again it's driving me mad? How can I use Core Data with no trouble, but not get the Navigation Controller to show. Sigh!

Thanks in advance.

Était-ce utile?

La solution 3

As it turned out, I had placed the code in a place where the window had not even been instantiated. So, that was the cause of all my problems. Thanks for all the answers provided though. Some of them really helped ...

Although still need to work out the difference between when to use ViewDidLoad and ViewWillLoad ...

Autres conseils

You need to do it in the AppDelegate, there is the only place you can set the rootView. If you do it in a UIViewController, you are probably setting a root from the root.

If you work with view controllers avoid at all cost manually adding or removing their views and try to use a higher level method such as switching UIWindow's rootController.

Advantages:

  • The SDK will take care of adding/removing/adjusting the actual views for you.
  • The view controller will get retained automatically.
  • Orientation changed, viewWillAppear: and all other controller-related events will get properly called.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top