self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[self.window addSubview:self.rootViewController.view]; //App will not crash without this line
self.navigationController = [[UINavigationController alloc] initWithRootViewController: self.rootViewController];
[self.window addSubview:self.navigationController.view];
I run it in the simulator and it crash, why?
Error message:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency',
reason: 'adding a root view controller <RootViewController: 0x6871620> as a child of
view controller:<UINavigationController: 0x6a86dc0>'
still have no idea
yours logic is wrong. Either add rootViewController or add navigationController as window's subview. You can't add two viewcontrollers at the same time. Here your navigationController will overwrite your rootviewcontroller. If possible then add your rootviewcontroller into navigationController or add navigationController into rootviewcontroller
Since You are setting RootViewController nib name to nil i hope you are managing your view in RootViewController.m inside method -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil or some other way.
to fix your error message you have given, change your posted code following way
self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController: self.rootViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
You are not specifying your problem clearly,so please add error message what you got on the console.Without error message we are unable to tell where the problem occur.Any how it crashes due to nibfile name,you specifying nib file name as nil.Please specify nib file name. Try this code once.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.navigationController=[[UINavigationController alloc] initWithRootViewController:self.rootViewController];
self.window.rootViewController = self.navigationController;
// [self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I hope this code will solve your problem
Related
The code I am using
NSLog(#"before Navigate to second activity");
Signature *temp = [[Signature alloc]initWithNibName:#"Signature" bundle:nil];
[self.navigationController pushViewController:temp animated:YES];
NSLog(#"after Navigate to second activity");
On console,both the log statements are getting printed, but my app is not navigating me to the next View.Please correct me.
if you are not using a UINavigationController on your app you can not call pushViewController, use presentViewController:animated:completion: instead:
[self presentViewController:temp animated:YES completion:nil];
for more information check the documentation
The log messages would show anyway, so no surprise there. Add a callback to the delegate for that navigation controller (you must first set a delegate of course) for
navigationController:didShowViewController:animated:
There you can make sure that the viewController passed (make sure Signature is a ViewController instance).
i think your application is not with navigationController so in AppDelegate.m file assign rootViewController like this..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RootViewController *viewController1 = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
UINavigationController *navviewController=[[UINavigationController alloc]initWithRootViewController:viewController1];
self.window.rootViewController = navviewController;
[self.window makeKeyAndVisible];
return YES;
}
after that check its working..
CreditsViewController *viewController = [[CreditsViewController alloc] init];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
you can try with above code
I'm using xCode 4.3.2 and started a blank application.
I have a navigation controller and a simple logincontroller. I want the login controller to be my root view so it is this first thing that a user does when they login.
I'm using the following code and when I run the application it displays a black screen. I put in logging in the LoginViewController.m->ViewDidLoad and it is being run is there something im doing wrong the LoginViewController.xib is very simple it just contains a button right now that will switch to a tab view controller once I figure this out.
Thanks in advance.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
UIViewController *loginController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyWindow];
return YES;
}
This is not right:
[self.window addSubview:navigationController.view];
change it to this:
self.window.rootViewController = navigationController;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why can't I push a new view controller onto the current view?
Why is my new view controller not appearing?
I need to push my view controller from my navigationController, but after having done an NSLog statement to find out why nothing was showing with the following code, I realised that it was returning null:
-(IBAction)doChangePasscode{
NSLog(#"Change Passcode Screen Loaded!");
ChangePasscode *cpscreen = [[ChangePasscode alloc] initWithNibName:#"ChangePasscode" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:cpscreen animated:YES];
NSLog(#"%#",self.navigationController);
}
Why is this happening? What can I do to return a proper value other than (null)?
Like everyone has explained in this and Why is my new view controller not appearing?, you need to have a navigation controller first. Use that nav controller to push your view controllers and then your view controllers will no longer have null for the navigationController property.
In this example someSecondViewController would be the self in your code above:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.nav = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
[rootViewController release];
[self.window addSubview:nav.view];
[self.window makeKeyAndVisible];
}
- (void)someOtherMethod {
SecondViewController *someSecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.nav pushViewController:someSecondViewController animated:YES];
[someSecondViewController release];
}
Please review these:
View Controller Programming Guide for iOS
UINavigationController Docs
UIViewController Docs
I am assuming you mean returning nil, that means the viewController does not have a navigation controller, you can solve it by having your viewController be the root of a newly created UINavigationController like this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myRootViewController];
Then for myRootViewController self.navigationController will return the navigation Controller.
I am assuming that viewController is the first one in your app so you want to have this on your appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyViewController *viewController = [[[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil] autorelease];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
self.window.rootViewController = myNavController;
[self.window makeKeyAndVisible];
return YES;
}
Then if that IBAction is on MyViewController self.navigationController will return the navigation Controller instead of nil.
Hope that helps.
I want to start my app up with a different Xib. How would I do this?
Thanks
If you're using interface builder:
Under SupportingFiles, in -info.plist, look for a key named "Main nib file base name". Change that to the XIB you want it to load first
You can also take that entry out of the plist altogether an in main.m give it your appDelegate's name:
int retVal = UIApplicationMain(argc, argv, nil, #"HelloViewAppDelegate");
Then in your appDelegate, you can manually load your first view controller based on your code and logic. Personally, I like this better because it's much clearer to - here's my delegate and code to load it. It doesn't have all the bindings in IB I need to remember.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
CGRect windowBounds = screenBounds;
windowBounds.origin.y = 0.0;
// init window
[self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];
// init view controller
_mainViewController = [[MainViewController alloc] init];
[[self window] addSubview:[_mainViewController view]];
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Answering your comment below. You pasted this invalid code:
// init view controller
ViewController = [[ViewController alloc] init];
[[self window] addSubview:[ViewController view]];
That's not valid. you need an instance variable name. By referring to it as "ViewController" your attempting to call class member variables. If your class is called ViewController then it should be:
// notice the instance myviewController is of type ViewController
ViewController *myViewController = [[ViewController alloc] init];
// notice calling view against instance (myViewController)
[[self window] addSubview:[myViewController view]];
At this point, if it's not compiling you need to edit your question and paste your main.m and appDelegate exactly as is into the question.
The MainWindow.xib file is just a shell that provides the application's UIWindow. While you can put a UIViewController in MainWindow.xib, you can also just have an unconnected UIViewController outlet on your app delegate and pick which nib to load at runtime in your application:didFinishLaunchingWithOptions: method, e.g.:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options {
if (case1)
self.viewController = [[[MyViewController1 alloc] initWithNibName:nil bundle:nil] autorelease];
else if (case 2)
self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
else
self.viewController = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[window addSubview:self.viewController.view];
[window makeKeyAndVisible];
return YES;
}
I have a UIView (menuView in code below) of size 320x218 inside a view. I want to load a navigation controller into this view. Im using the following code to do that:
MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:menuController];
navigationController.navigationBarHidden = YES;
[menuView addSubview:navigationController.view];
[menuController release];
[navigationController release];
When I execute it, the root view is not displayed in that view. Only a navigation bar is displayed and the rest of the view is empty.
Edit:
I just placed an NSLog() in both initWithNibName: and viewDidLoad: of MenuViewController. The one in initWithNibName: gets called but the one in viewDidLoad: doesn't :S
Update:
I tried to push menuController to my navigationController thinking since its not appearing, it might not be on the stack. Exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
call layoutsubviews do work.
[super loadView];
[self.view addSubview:navigationController.view];
[navigationController.view layoutSubviews];
I found the answer here:
UIViewController -viewDidLoad not being called
I had to add these lines of code after -initWithRootViewController in order to load the view of my root view Controller:
navigationController.navigationBarHidden = YES;
[navigationController setView:menuController.view];
You should not add the navigationViewController as an subview To your MenuViewController.
As the navigationViewController already already holds the MenuViewController.
Just display the navigationViewController.
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
ViewController *viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
navController = [[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController = self.navController;
Try this code in your appdelegate method