开发APP免不了需要使用导航栏,可以方便的添加左右按钮方便返回操作,那么怎么添加呢,总是记不住,每次要查资料,还是写了demo记录下来才是王道。
第一步在APPDelegate里面的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 函数里面添加添加如下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { PathViewController *controller = [[PathViewController alloc] initWithNibName:@"PathViewController" bundle:nil]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.mUINavigationController = [[UINavigationController alloc] init]; [self.mUINavigationController pushViewController:controller animated:YES]; [self.window addSubview:self.mUINavigationController.view]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
PathViewController是你的工程里面的某个页面,通过navigationController来加载到window里面,就显示出来了。
此时的PathViewController并没有定义leftBar和rightBar以及title,那么我们下面来添加。
第二步:为PathViewController添加title,此时要在PathViewController的viewWillAppear函数里面添加
有两种方式来添加title;
第一种方式是:
self.title = @"title";第二种方式是:
self.navigationItem.title = @"titleof";
第三步:为PathViewController添加leftBar
代码如下:需要首先定义一个UIBarButtonItem,然后添加到navigationItem中去。运行就可以显示Exit的按钮显示在页面的左上方。
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"Exit" style:UIBarButtonItemStyleBordered target:self action:@selector(exitAPP)]; self.navigationItem.leftBarButtonItem = leftItem;
exitAPP的函数如下:
-(void)exitAPP{ exit(0); }
第四步:为PathViewController添加一个标题说明
self.navigationItem.prompt = @"Hello, I'm the prompt";这个标题说明会显示在title的上面,以小号字体显示。
好了,导航的介绍大致就这么多。