Search code examples
iosframeviewdidloadviewwillappear

Viewdidload working, while viewwillappear is delayed


I have a project in which I'm switching one view with another:

- (IBAction)onClick:(id)sender
{
ViewControllerSecond * sc=[[ViewControllerSecond alloc]initWithNibName:@"ViewControllerSecond" bundle:nil];
[UIView transitionFromView:self.view toView:sc.view duration:3.0
                   options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {

}];
}

I'm using 3 seconds here to make a point. in this second view I have a method to update the GUI that adds another view from a view controller:

    -(void)updateGUI
{

    sample=[[ViewControllerSample alloc]initWithNibName:@"ViewControllerSample" bundle:nil];
    sample.view.frame=CGRectOffset(sample.view.frame, 0, 150);
    [self.view addSubview:sample.view];
}

Now, here is the problem: when I'm calling this from the viewDidLoad function - it's working just fine. However, if called from the viewWillAppear function, the view will appear at the top of the screen and only after the animation has ended will jump to it's position. How can it be fixed?


Solution

  • The answer for that was of two parts:

    1. update when the view is loaded, however this will only happen once, so to re-use the controller I have to call it again.
    2. use the isViewLoaded to see if the view is loaded. if it is, then call the update method.