tags:

views:

197

answers:

1

Going through many tutorials and with the help of everyone here, I am trying to wrap my head around using multi view controllers with their own xib files.

I have one example where there is a : multiViewController and two others: aboutViewController, rulesViewController.

In both aboutViewController.m and rulesViewController.m files, I have placed the following code:

- (void)viewDidLoad {
NSLog(@"rules View did load"); // (Or About View did load, depending on the .m file)
[super viewDidLoad];
 }

The mainViewController.m file contains:

-(IBAction) loadRules:(id) sender {
[self clearView];
[self.view insertSubview:rulesViewController.view atIndex:0];
}

-(IBAction) loadAbout:(id) sender {
[self clearView];
[self.view insertSubview:aboutViewController.view atIndex:0];

}

My question is, why is it when I run the application does the ViewDidLoad for both About and Rules fire? Meaning, I get the NSLog messages. Does this mean that regardless of the separate xib files, all views are loaded on start up?

My point of all this is: I want the multiViewController to be my main menu which will have separate buttons for displaying the other xib files when clicked. I had been placing my "initalize" code in the viewDidLoad but this seems wrong now as I don't want to hit until the users presses the button to display that view.

An example was to have a view that is: playGameViewController. In the viewDidLoad I was checking to see if a prior game was in progress and if so, prompt the user if they would like to resume. In the above case, when the app starts, it prompts right away (because the viewDidLoad is firing) even though I only wanted to display the main menu first.

Any explanation is greatly appreciated. Thanks in advance.

Geo...

A: 

My question is, why is it when I run the application does the ViewDidLoad for both About and Rules fire? Meaning, I get the NSLog messages. Does this mean that regardless of the separate xib files, all views are loaded on start up?

When you call

[self.view insertSubview:rulesViewController.view atIndex:0];

it's going to first call viewDidLoad for the initial view and then viewDidLoad once again for RulesViewController.

When your MainViewController, or any view for that matter loads, viewDidLoad is called automatically. ViewDidLoad is there in order for you to override or modify any objects in the nib, or you can create objects yourself. Views are only loaded on an as needed basis. If you were to load all your views initially when the app boots up, the user would just see a black screen until all the views are processed.

You say you are going through some tutorials, I don't know your area of expertise yet, but have you looked into navigation based apps using UINavigationController?

Just an example as your request if you want to have a button load a view you can do something like.

- (IBAction)pushSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    if (secondView != nil) {
        secondView.title = @"Second View";
        [self.navigationController pushViewController: secondView animated: YES];
    }
}
Convolution
If this is the case, why would the viewDidLoad for the other two classes (loadAbout and loadRules) "fire" on startup of the multiViewController. The multiViewController contains the self.view insertSubview but only when theywould be accessed via a button click...or am I reading this wrong?
George
In IB, what are your action methods connected to? Do you know what object is sending the signal to commit these actions?
Convolution
In IB, there are two buttons, each connected via the Touchup inside to either the loadRules or the loadAbout.This is the part I am not understanding. Why is it if they are only suppose to "fire" when they are clicked, are the loadDidView's being "fired" when the app is run.
George
Your action methods and viewdidload are fine, but something else maybe in the app delegate (maybe), are causing some sender to execute those actions. Can you use the tag property or something else to figure out the origin of the sender? I've emulated this exact code a few times and I can't get it to do what you're describing.
Convolution