In short: I want to have two full-screen views where I can switch between view A and view B. I know that I can just use the tab bar controller, but I don't want that. I want to see how this is done manually to find out what is happening under the hood.
I have a UIViewController that acts as a root controller:
@interface MyRootController : UIViewController { IBOutlet UIView *contentView; } @property(nonatomic, retain) UIView *contentView; @end
ContentView connects to the UIView, which I added as a view to the Nib "view". It is green and I see it in full screen. It works great.
Then I created two other View Controllers in much the same way. ViewControllerA and ViewControllerB. ViewControllerA has a blue background, ViewControllerB has a black background. Just to see which one is active.
So, in the implementation of myRootController, I do this:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; ViewControllerA *vcA = [[ViewControllerA alloc] initWithNib]; [self.contentView addSubview:vcA.view]; [cvA release]; }
By the way, the -initWithNib method looks like this:
- (id)initWithNib { // Load the view nib if (self = [super initWithNibName:@"ViewA" bundle:nil]) { // do ivar initialization here, if needed } return self; }
It works. I see the ViewControllerA view when the application starts. But now the big question is: A View Controller usually has all such methods as:
- (voids) viewWillAppear: (BOOL) animated;
- (voids) viewDidDisappear: (BOOL) animated;
- (voids) viewDidLoad;
... and so on. Who or what, or how will these methods be called if I make it my way without a tab bar controller? I mean: if I highlighted this ViewController class and the view became visible, would I need to take care of calling these methods? How does he know that viewWillAppear, viewDidDisappear or viewDidLoad? I believe that the Tab Bar Controller has all this “skill” under the hood. Or am I wrong?
UPDATE: I tested it. If I release the view controller (for example: ViewControllerA), I will not receive the log message in viewDidDisappear. Only when distributing and initializing ViewControllerA do I get viewDidLoad. But it is so. So, all the signs mean UITabBarController smart now;) and I need to figure out how to do this, right?
iphone uikit uiviewcontroller uitabbarcontroller
Thanks May 26 '09 at 2:30 p.m. 2009-05-26 14:30
source share