导航栏: UINavigationBar用法

来源:雨松MOMO

利用代码在屏幕中添加一个标题栏,并且在标题栏左右两方在添加两个按钮,点击后响应这两个按钮。
这里设置标题栏的显示范围。

1 //创建一个导航栏
2 UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

  有了标题栏后,须要在标题栏上添加一个集合Item用来放置 标题内容,按钮等。

1 //创建一个导航栏集合
2 UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];

在这个集合Item中添加标题,按钮。
style:设置按钮的风格,一共有3中选择。
action:@selector:设置按钮点击事件。

01 //创建一个左边按钮
02   UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边"
03                                                             style:UIBarButtonItemStyleBordered
04                                                             target:self
05                                                             action:@selector(clickLeftButton)]; 
06  
07   //创建一个右边按钮
08   UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"
09                                                                 style:UIBarButtonItemStyleDone
10                                                                 target:self
11                                                                 action:@selector(clickRightButton)];
12   //设置导航栏内容
13   [navigationItem setTitle:@"雨松MOMO程序世界"];

将标题栏中的内容全部添加到主视图当中。

1 //把导航栏添加到视图中
2 [self.view addSubview:navigationBar];

最后将控件在内存中释放掉,避免内存泄漏。

1 //释放对象
2 [navigationItem release];
3 [leftButton release];
4 [rightButton release];

如图所示:添加这两个按钮的点击响应事件。

 

 

01 -(void)clickLeftButton
02 {
03  
04     [self showDialog:@"点击了导航栏左边按钮"];
05  
06 }
07  
08 -(void)clickRightButton
09 {
10  
11     [self showDialog:@"点击了导航栏右边按钮"];
12  
13 }

 

 

 

 

点击后打开一个Dialog对话框,根据点击不同的按钮传入不同的显示内容。

 

1 -(void)showDialog:(NSString *) str
2 {
3  
4     UIAlertView * alert= [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];   
5  
6     [alert show];
7     [alert release];
8 }

最后贴上完整的代码

 

01 #import "TitleViewController.h"
02  
03 @implementation TitleViewController
04  
05 - (void)didReceiveMemoryWarning
06 {
07     // Releases the view if it doesn't have a superview.
08     [super didReceiveMemoryWarning];
09  
10     // Release any cached data, images, etc that aren't in use.
11 }
12  
13 #pragma mark - View lifecycle
14  
15 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
16 - (void)viewDidLoad
17 {
18     [super viewDidLoad];
19  
20     //创建一个导航栏
21     UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
22  
23     //创建一个导航栏集合
24     UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil]; 
25  
26     //创建一个左边按钮
27     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边"
28                                                               style:UIBarButtonItemStyleBordered
29                                                               target:self
30                                                               action:@selector(clickLeftButton)]; 
31  
32     //创建一个右边按钮
33     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"
34                                                                   style:UIBarButtonItemStyleDone
35                                                                   target:self
36                                                                   action:@selector(clickRightButton)];
37     //设置导航栏内容
38     [navigationItem setTitle:@"雨松MOMO程序世界"];
39  
40     //把导航栏集合添加入导航栏中,设置动画关闭
41     [navigationBar pushNavigationItem:navigationItem animated:NO];
42  
43     //把左右两个按钮添加入导航栏集合中
44     [navigationItem setLeftBarButtonItem:leftButton];
45     [navigationItem setRightBarButtonItem:rightButton];
46  
47     //把导航栏添加到视图中
48     [self.view addSubview:navigationBar]; 
49  
50     //释放对象
51     [navigationItem release];
52     [leftButton release];
53     [rightButton release];
54  
55 }
56  
57 -(void)clickLeftButton
58 {
59  
60     [self showDialog:@"点击了导航栏左边按钮"];
61  
62 }
63  
64 -(void)clickRightButton
65 {
66  
67     [self showDialog:@"点击了导航栏右边按钮"];
68  
69 }
70  
71 -(void)showDialog:(NSString *) str
72 {
73  
74     UIAlertView * alert= [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];   
75  
76     [alert show];
77     [alert release];
78 }
79  
80 - (void)viewDidUnload
81 {
82     [super viewDidUnload];
83     // Release any retained subviews of the main view.
84     // e.g. self.myOutlet = nil;
85 }
86  
87 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
88 {
89     // Return YES for supported orientations
90     return (interfaceOrientation == UIInterfaceOrientationPortrait);
91 }
92  
93 @end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值