views:

18

answers:

0

I know this might be very simple but what is the difference between these two pieces of code? I know that the second method adds a subview, and that the second code loads the BlueViewController on load and then it does a lazy load with the YellowViewController class (if it's not loaded on the RootViewController). What are the advantages/disadvantages of these two methods?

First:

-(IBAction)switchViews{
    Test2 *screen = [[Test2 alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

Second:

-(void)viewDidLoad{
    BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];
    self.blueViewController = blueController;
    [self.view insertSubview:blueController.view atIndex:0];
    [blueController release];
}
-(IBAction)switchViews:(id)sender{
    if (self.yellowViewController == nil)
    {
        YellowViewController *yellowController = 
        [[YellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil];
        self.yellowViewController = yellowController;
        [yellowController release];
    }

if (self.blueViewController.view.superview == nil)
    {
        [blueViewController viewWillAppear:YES];
        [yellowViewController viewWillDisappear:YES];

        [yellowViewController.view removeFromSuperview];
        [self.view insertSubview:blueViewController.view atIndex:0];
    }
    else {
        [yellowViewController viewWillAppear:YES];
        [blueViewController viewWillDisappear:YES];

        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:yellowViewController.view atIndex:0];
    }

}