我们常常会遇到这样的问题:一个应用会有几个视图控制器没有导航栏,那么我们只需要在不需要导航栏的页面将其隐藏。
代码如下:
#import <UIKit/UIKit.h>
@class NavController;
@class FirstController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
//第一次出现导航栏的页面
@property (nonatomic , retain)NavController *controller;
//导航栏视图控制器
@property (nonatomic , retain)UINavigationController *navgationController;
//系统第一次出现的页面,没有导航栏
@property (nonatomic , retain)FirstController *firstController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// self.controller = [[NavController alloc] initWithNibName:nil bundle:nil];
self.firstController = [[FirstController alloc] initWithNibName:nil bundle:nil];
//将第一个页面加入到导航栏根视图结点,然后在firstController中将导航栏隐藏
self.navgationController = [[UINavigationController alloc] initWithRootViewController:self.firstController];
[self.window addSubview:self.navgationController.view];
return YES;
}
FirstViewController.m
- (void) buttonPressed
{
self.controller = [[NavController alloc] initWithNibName:nil bundle:nil];
[self.view removeFromSuperview];
[self.view addSubview:self.controller.view];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//隐藏第一个页面的Navigation
self.delegate = [UIApplication sharedApplication].delegate;
self.delegate.navgationController.navigationBarHidden = YES;
UIImage *image = [UIImage imageNamed:@"loginBackgroud.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 100, 200, 140)];
[button setTitle:@"NEXT" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imageView];
[self.view addSubview:button];
}
NavController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//调用获取
self.delegate = [UIApplication sharedApplication].delegate;
self.delegate.navgationController.navigationBarHidden = NO;
self.delegate.navgationController.visibleViewController.title= @"NavController";
self.controller = [[NextController alloc] initWithNibName:nil bundle:nil];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"next"style:UIBarButtonItemStylePlain target:self action:@selector(nextPressed)];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStylePlain target:self action:@selector(backPressed) ];
self.delegate.navgationController.visibleViewController.navigationItem.rightBarButtonItem = rightItem;
self.delegate.navgationController.visibleViewController.navigationItem.leftBarButtonItem = leftItem;
// [rightItem release];
}
在iPhone中对应的是UIApplication在接收到生命周期和系统事件后委托给UIApplicationDelegate来处理。每个项目都会有一个UIApplication对象来处理应用程序的生命周期和系统事件,main()函数通过UIApplicationMain()来初始化应用程序的UIApplication,如果想对应用程序的UIApplication进行操作,就只能通过[UIApplication sharedApplication]来获取到UIApplication的引用,这个方法会返回一个全局唯一的UIApplication对象给读者。
同样,读者可以通过如下代码获取应用程序的委托对象:UIApplicationDelegate* myDelegate = [[UIApplication sharedApplication] delegate];
NextController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"NextController";
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"aaa"style:UIBarButtonItemStylePlain target:self action:@selector(nextPressed)];
self.navigationItem.rightBarButtonItem = rightItem;
self.navigationItem.backBarButtonItem = rightItem;
[self.navigationItem setHidesBackButton:YES];
[rightItem release];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setFrame:CGRectMake(15, 5, 38, 38)];
UIImage *image = [UIImage imageNamed:@"button_down.png"];
[leftButton setImage:image forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(backPressed)forControlEvents:UIControlEventTouchUpInside];
//通过initWithCustomView可以自定义导航栏
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = backItem;
}
LastController.m
- (void)viewDidLoad
{
[super viewDidLoad];
//隐藏导航栏
[self.navigationController setNavigationBarHidden:YES animated:YES];
UIImage *image = [UIImage imageNamed:@"loginBackgroud.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:imageView];
}
最后,navgationBar添加图片方法如下:
[self.navgationController.navigationBar setBackgroundImage:[UIImageimageNamed:@"button_down.png"] forBarMetrics:UIBarMetricsDefault];