4

Before the user can use my application he has to login. After he logged in, the database is built because I need information from the server to build it.

Therefore my root ViewController is the LoginViewController which presents the actual application (a navigationController stack) modally on successful login.

If the user is already logged in on application launch (I am storing the credentials with NSUserDefaults) the LoginViewController should present the application immediately. Therefore I overwrote the method:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSInteger userId = [[NSUserDefaults standardUserDefaults] integerForKey:@"selfUser"];
    if (userId != 0) {
        //[self performSelector:@selector(presentMainViewController) withObject:nil afterDelay:2];
        [self presentMainViewController];
    }
}
- (void)presentMainViewController {
    mainViewController = [[MainViewController alloc] init];
    mainViewController.managedObjectContext = managedObjectContext;
    navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
    navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:navigationController animated:NO];
}

The [self presentMainViewController]; is executed but the controller does not show up. If I use the line above it does work.

Where do I have to put the code to make it work?

PengOne
  • 48,188
  • 17
  • 130
  • 149
Alexander Theißen
  • 3,799
  • 4
  • 28
  • 35
  • have you try present the login on the delegate? You could present the login at the start, then if the user is already logged present the application. – Mat Jun 19 '11 at 23:25
  • Can you post the method body for `presentMainViewController` please? From my own experience both -viewDidAppear and -viewWillAppear can work for presenting modal view controllers. – axiixc Jun 20 '11 at 03:11
  • @mat I don't understand. I DO present the loginViewController from the application delegate and present the application from the loginViewController. @axiixc done – Alexander Theißen Jun 20 '11 at 16:32
  • If you call `performSelector:...` with a delay of 0, does it also work? – Morten Fast Jun 20 '11 at 18:14
  • Yes it does! But there has to be a bug somewhere. Shouldn't it work without this "trick"? – Alexander Theißen Jun 20 '11 at 18:17
  • It's not a trick. :) By calling performSelector with a delay (even a delay of 0), you're moving the execution to the run loop's next run, and then the current view stack has been completely set up. – Morten Fast Jun 20 '11 at 18:26

2 Answers2

4

The view stack might not be completely created when viewDidAppear is send. So you should use the perfomSelector:withDelay to queue the call on the run loop. In this way you can ensure that the view stack is build when your code runs.

Cheers!

Stuck
  • 11,225
  • 11
  • 59
  • 104
0

I had a similar situation and I resolved by moving the code to viewWillAppear (instead of viewDidAppear). It may worth to try.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
nnahum
  • 364
  • 6
  • 12
  • 2
    Does not work either. I created a button which does call [self presentMainViewController];, too. The button works fine. The funny thing is: If I put the above code in viewDidAppear the button does not work, eiter. In viewWillAppear the button works. But this does not solve my problem, of course. – Alexander Theißen Jun 19 '11 at 23:07