When you push or pop a view controller on/off a navigaction controller's stack, the usual viewWillAppear / viewDidappear methods aren't called. If you want to ensure they're always called, just add the UINavigationControllerDelegate protocol to your root view controller:
@interface RootViewController : UIViewController <UINavigationControllerDelegate> {
UINavigationController *navController;
}
Then implement these two methods:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [viewController viewWillAppear:animated]; } - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [viewController viewDidAppear:animated]; }
Be sure to set the root view controller as the delegate for the nav controller. Now viewWillAppear / viewDidAappear will be called whenever a controller is pushed/popped from the stack.
If you want to call the viewWillDisappear/viewDidDisappear methods, your view controller still has to do that manually before popping itself off the nav stack.
.H
//
// landpokerViewController.h
// landpoker
//
// Created by zltianhen on 11-9-15.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface landpokerViewController : UIViewController <UINavigationControllerDelegate>
{
//页面堆栈的控制
UINavigationController* _gamePageManage;
}
+ (landpokerViewController*)shareLandpoker;
@end
.M
//
// landpokerViewController.m
// landpoker
//
// Created by zltianhen on 11-9-15.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "landpokerViewController.h"
#import "LoginViewController.h"
#import "PageManage.h"
landpokerViewController* g_landpokerView=nil;
@implementation landpokerViewController
+ (landpokerViewController*)shareLandpoker
{
return g_landpokerView;
}
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
{
// Custom initialization
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];
g_landpokerView = self;
if (!_gamePageManage)
{
//LoginViewController是栈顶
LoginViewController* login = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
_gamePageManage = [[UINavigationController alloc] initWithRootViewController:login];
[_gamePageManage setNavigationBarHidden:YES];
[PageManage sharePageManage].navigation = _gamePageManage;
[login release];
_gamePageManage.delegate = self;
[self.view addSubview:_gamePageManage.view];
}
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft)) {
return YES;
}
return NO;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[_gamePageManage release];
[super dealloc];
}
- (void)viewWillAppear:(BOOL)animated // Called when the view is about to made visible. Default does
{
}
- (void)viewDidAppear:(BOOL)animated
{
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
}
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewDidAppear:animated];
}
@end