UINavigationController用法
新建一个Empty项目后,在application:didFinishLaunchingWithOptions:中的代码如下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
FirstController *controller = [[FirstController alloc] init];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
[controller release];
self.window.rootViewController = navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
self.window.rootViewController = navigationController;与
[self.window addSubview:navigationController.view];的区别:
前者是新的用法,在ios4之后新出现,后者是旧的写法。
这个应用的生命周期就只有一个navigationController,如果在用push进来的各个ViewController里的self.navigationController都是指同一个navigationController。
隐藏导航栏:
navigationController.navigationBarHidden =YES; (推荐用这个)
或navigationController.navigationBar.hidden = YES;(不推荐用这个,因为如果把hidden设成YES,导航栏隐藏掉,再把hidden设成NO,导航栏并没有显示出来,用前一句代码不会出现此问题)
如果显示了导航栏,那么所有的ViewController的view在屏幕中都下移了一个导航栏的高度,因此如果把控件放在view的最底部,有可能会被遮住。为了预防这个问题,在xib的"Top Bar"的值设为"Navigation Bar",这样的好处是提醒实际运行中是有一个导航栏的,如果设置了这个,self.view.frame.size.height的值也变小了,这时是416(因为480-20-44=416)。
ViewController导航栏的标题:
self.navigationItem.title =@"标题";
ViewController导航栏的view:
self.navigationItem.titleView = view;
ViewController导航栏隐藏返回按钮:
self.navigationItem.hidesBackButton = YES;
ViewController导航栏的prompt:
self.navigationItem.prompt =@"prompt test";
ViewController导航栏的左右按钮:
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonSystemItemCanceltarget:selfaction:@selector(clickBack:)];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
UIBarButtonItem *rightButton = [[UIBarButtonItemalloc]initWithTitle:@"确认"style:UIBarButtonSystemItemSavetarget:selfaction:@selector(clickSure:)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
说说以下这两句的区别
self.title = @"abc";
self.navigationItem.title = @"1111";
实际上导航栏的标题只会显示 self.navigationItem.title的值,但如果我们不给self.navigationItem.title赋值,那为什么导航栏会显示self.title的值呢,因为当self.navigationItem.title没值时,会自动把self.title的值赋给self.navigationItem.title。 UINavigationController返回总结
[self.navigationController popViewControllerAnimated:YES]; 弹出当前视图控制器
[self.navigationController popToViewController:viewController animated:YES]; 弹出到指定视图控制器(回到指定视图控制器, 也就是不只弹出一个)
[self.navigationController popToRootViewControllerAnimated:YES]; 弹出到根视图控制
假设我的一个navigationController里共有4个viewcontroller,想要从第5层直接返回到第2层或第3层,则没有现成的方法可以调用了。但这时若能够知道pop回去的ViewController的指针,也就好办了。具体写法如下:
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] animated:YES];
在使用时,根据自己返回层的需要,只要改变一下“-2”这个数字就可以达到目的了。
如果UINavigationController不是push过来的
if ([self.navigationController.viewControllersindexOfObject:self] ==0)
{
UIBarButtonItem *backButton = [[UIBarButtonItemalloc] initWithTitle:NSLocalizedString(@"取消",@"") style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
[backButtonrelease];
}
则返回方法如下写法
- (void)goBack
{
if ([self.navigationController.viewControllerscount] <= 1) {
[selfdismissModalViewControllerAnimated:YES];
}
else {
[self.navigationControllerpopViewControllerAnimated:YES];
}
}