0

I have a root view controller which should load another view controller as soon as it is done loading (i.e. in the viewDidLoad method).

I am using the UINavigationController in order to push a new view controller onto the stack:

In my rootviewcontrollerappdelegate:

-(void) viewDidLoad{
        LoginViewController* lvc = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:lvc animated:NO];
}

I have textfields and buttons in the view controller to be loaded. The above doesn't seem to work however...It loads just a blank grey screen and no UINavigation bar is present. If I comment out the second line (pushViewController line), then I see the navigation bar. So I think it is loading something, but the items in the view controller being loaded are not being shown...Any ideas why?

ucabdro
  • 23
  • 1
  • 5

4 Answers4

1

Check if navigationController is pointing to nil. If it does, try

[self.view addSubview:self.pushViewController.view]

I had the same problem and found the above solution here: UIViewController -viewDidLoad not being called

Community
  • 1
  • 1
das_weezul
  • 6,082
  • 2
  • 28
  • 33
  • My viewDidLoad is being called but its not pushing the view correctly, its loading a blank view....NSLogging the navigation controller also shows that it is pointing to an instance of UINavigationController – ucabdro Jul 25 '11 at 22:53
  • in your viewDidLoad method, are you adding all the subviews to the view correctly? – Maggie Jul 26 '11 at 12:04
1

Unless you're doing something tricky, you should be calling alloc on the LoginViewController class rather than a variable. Also, if you've set up LoginViewController in Interface Builder (as opposed to programmatically), you'll need to load it from an NIB:

LoginViewController *lvc = [[[LoginViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[self.navigationController pushViewController:lvc animated:NO];

Have a look at initWithNibName:bundle: in the docs.

  • I don't think what you are saying is 100% correct. It is possible to set the File's Owner Class to point to the correct view controller. Connecting the File's Owner view to the View object in the nib is usually all I have to do to get this working. I have never tried to use it within the viewDidLoad method though...I think my problem lies here somewhere?! – ucabdro Jul 25 '11 at 23:04
  • You can set the class of the File's Owner object but, as far as I am aware, this is just so you can access `IBOutlet`s and the ilk. It is my understanding that you must name the NIB that is to be used by the view controller; `init` doesn't do it for you. The docs state: "If you specify views using a nib file, you must [...] initialize your view controller object using the `initWithNibName:bundle:` method." –  Jul 26 '11 at 00:38
  • The docs say if `nil` is passed for the nib name then it will look for a xib file with the same name as the view controller. – Paul.s Jul 27 '11 at 18:54
0

Not entirely sure what you are trying to achieve but when you instantiate LoginViewContoller it should probably look like this

LoginViewController* lvc = [[LoginViewController alloc]init];
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • Then lvc is definately pointing to nil ;) Who came up with the smart idea of not throwing an exception when you're passing messages to nil anyway? ;) – das_weezul Jul 25 '11 at 22:48
0

Judging by the nature of your naming for your view controller, is your LoginViewController the first view controller for your UINavigationController?

If that is what you're trying to do, you should instead initialise your navigation controller with the LoginViewController as the root controller instead of pushing it onto the navigation stack.

UINavigationController has a method to do this:

- (id)initWithRootViewController:(UIViewController *)rootViewController

EDIT:

Well, one way you can go about it is like this.

In your application delegate .h file, you should have declared a UINavigationController.

@interface MyAppDelegate : NSObject <UIApplicationDelegate> 
{
    UINavigationController *navController;
}

@property (nonatomic, retain) UINavigationController *navController;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

In your App Delegate didFinishLaunching:withOption: you can create an instance of your LoginViewController there, and use that to init your UINavigation controller as the root view controller

#import "LoginViewController.h"

@implementation MyAppDelegate

@synthesize navController;
@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    LoginViewController *loginController = [[LoginViewController alloc] init];

    navController = [[UINavigationController alloc] initWithRootViewController:loginController];

    [loginController release];

    [[self window] setRootViewController:navController];

    [navController release];

    [self.window makeKeyAndVisible];
    return YES;
}

I probably have a typo here or there but that's one way I would go about doing it.

Zhang
  • 11,549
  • 7
  • 57
  • 87
  • Yes it is. But I want the rootviewcontroller to load the loginviecontroller as soon as the app has loaded. How is this possible? – ucabdro Jul 26 '11 at 09:43
  • Just a side question in addition to my above edits, do you want the Login View inside the navigation controller or do you want the user to see the login first, then after login, present them with another view controller that is inside a navigation? – Zhang Jul 26 '11 at 12:14