1

I have the following code in my AppDelegate:

#import <UIKit/UIKit.h>

@class PersonalDiarySystemViewController;

@interface PersonalDiarySystemAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    PersonalDiarySystemViewController *viewController;
    UINavigationController *navigationController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet PersonalDiarySystemViewController *viewController;
@property (nonatomic, retain) UINavigationController *navigationController;

@end

#import "PersonalDiarySystemAppDelegate.h"
#import "PersonalDiarySystemViewController.h"

@implementation PersonalDiarySystemAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize navigationController;

#pragma mark -
#pragma mark Application lifecycle

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

    // Override point for customization after application launch.

    // Set the view controller as the window's root view controller and display.
    self.window.rootViewController = self.viewController;
    navigationController = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];  
    navigationController.navigationBar.tintColor = [UIColor   
                                                         colorWithRed:217.0/255   
                                                         green:33.0/255 
                                                         blue:0   
                                                         alpha:1];
    navigationController.navigationBarHidden = YES;
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

My rootviewcontroller tries to load another viewcontroller into the navigation controllers stack in its viewDidLoad method but for some reason the view is not getting pushed:

-(void) viewDidLoad{    
    lvc = [[LoginViewController alloc] init];
    //lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [lvc setDelegate:self]; 
    //[self presentModalViewController:lvc animated:YES];   
    [self.navigationController pushViewController:lvc animated:YES];    
}

I'm getting no errors so not sure whats going on...using presentModalViewController works...so really am confused!!

Venk
  • 5,949
  • 9
  • 41
  • 52
user559142
  • 12,279
  • 49
  • 116
  • 179

4 Answers4

2

You need to assign lvc to LoginViewController.

- (void) viewDidAppear
{    
    [self performSelector:@selector(loginCheck:) withObject:nil afterDelay:0.5];
}    
- (void) loginCheck:(id)sender
{
    LoginViewController * lvc = [[LoginViewController alloc] init];
    //lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [lvc setDelegate:self]; 
    //[self presentModalViewController:lvc animated:YES];   
    [self.navigationController pushViewController:lvc animated:YES];   
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
1

Put your

[self.navigationController pushViewController:loginViewController];

Into the

- (void)viewDidAppear:(BOOL)animated 

method. The viewControllers navigationController doesn't get loaded until then

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
Flippinjoe
  • 61
  • 2
0

There are two things that might go wrong. First, you alloc the navigation controller in applicationDidFinishLaunching, I'm not quite sure which goes first, applicationDidFinishLaunching or viewDidLoad.

As you've seen, you first set your root view controller, then alloc the navigation controller, then maybe viewDidLoad launched right after you set the root view controller, then the navigation controller is allocated. so the words in viewDidLoad may not work because at that time, the navigation controller hasn't been born yet.

But I don't' quite thing the previous explanation works. it's just a possibility.

There's another strange thing, you set the navigation bar of your navigation controller hidden

navigationController.navigationBarHidden = YES;

Then it seems that the user can't pop back to the root view controller, so the navigation controller don't push the login view controller. Meanwhile, the modal view controller can be dismissed with the navigation bar hidden or the navigation bar not allocated, so it works when you present it as a modal view controller.

but i'm still not quite sure about it since i'm now having some issues with Xcode, so i can't test the previous two ideas, sorry about that. but i still recommend that you set navigationBarHidden to NO.

Venk
  • 5,949
  • 9
  • 41
  • 52
Lewen
  • 1,823
  • 2
  • 15
  • 17
0
- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.navController = navigationController;
    [mainViewController release];
    [navigationController release];

    // Configure and display the window.
    [window addSubview:navController.view];
    [window makeKeyAndVisible];
}

- (void)viewDidLoad{    
    lvc = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:lvc animated:YES];    
}
Bannings
  • 10,376
  • 7
  • 44
  • 54
ram
  • 1,193
  • 1
  • 15
  • 27