IOS 入门开发之创建标题栏UINavigationBar的使用

时间 : 15-09-10 栏目 : iOS, 移动开发 作者 : noway 评论 : 0 点击 : 328 次

OS 开发有关界面的东西不仅可以使用代码来编写,也可以使用Interface Builder可视化工具来编写。今天有个朋友问我这两个有什么区别,首先说 说IB ,使用它编辑出来的控件其实底层还是调用代码只是苹果封装出来让开发者更好使用而已。它的优点是方便、快捷最重要的是安全,因为控件的释放它会帮我们完成 不用手动释放。缺点是多人开发不好维护,就好比谁写的IB谁能看懂,别人看的话就比较费劲,不利于代码的维护。两种方式各有利弊,不过我个人还是比较喜欢 纯代码,因为任何程序语言,或者任何脚本语言,代码和可视化工具比起来永远是最底层的.


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

  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:设置按钮点击事件。

  1. //创建一个左边按钮 
  2.   UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边"    
  3.                                                             style:UIBarButtonItemStyleBordered    
  4.                                                             target:self    
  5.                                                             action:@selector(clickLeftButton)];   
  6.  
  7.   //创建一个右边按钮 
  8.   UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"    
  9.                                                                 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]; 

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

  1. -(void)clickLeftButton 
  2.      
  3.     [self showDialog:@"点击了导航栏左边按钮"]; 
  4.    
  5.  
  6.  
  7. -(void)clickRightButton 
  8.      
  9.     [self showDialog:@"点击了导航栏右边按钮"]; 
  10.      

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

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

最后贴上完整的代码

  1. #import "TitleViewController.h" 
  2.  
  3. @implementation TitleViewController 
  4.  
  5. - (void)didReceiveMemoryWarning 
  6.     // Releases the view if it doesn't have a superview. 
  7.     [super didReceiveMemoryWarning]; 
  8.      
  9.     // Release any cached data, images, etc that aren't in use. 
  10.  
  11. #pragma mark - View lifecycle 
  12.  
  13.  
  14. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
  15. - (void)viewDidLoad 
  16.     [super viewDidLoad]; 
  17.  
  18.  
  19.     //创建一个导航栏 
  20.     UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];   
  21.      
  22.     //创建一个导航栏集合 
  23.     UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];   
  24.      
  25.     //创建一个左边按钮 
  26.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边"    
  27.                                                               style:UIBarButtonItemStyleBordered    
  28.                                                               target:self    
  29.                                                               action:@selector(clickLeftButton)];   
  30.  
  31.     //创建一个右边按钮 
  32.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"    
  33.                                                                   style:UIBarButtonItemStyleDone    
  34.                                                                   target:self    
  35.                                                                   action:@selector(clickRightButton)];   
  36.     //设置导航栏内容 
  37.     [navigationItem setTitle:@"雨松MOMO程序世界"]; 
  38.      
  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.     //释放对象 
  52.     [navigationItem release];   
  53.     [leftButton release];   
  54.     [rightButton release]; 
  55.  
  56.  
  57.  
  58. -(void)clickLeftButton 
  59.      
  60.     [self showDialog:@"点击了导航栏左边按钮"]; 
  61.    
  62.  
  63.  
  64. -(void)clickRightButton 
  65.      
  66.     [self showDialog:@"点击了导航栏右边按钮"]; 
  67.      
  68.  
  69.  
  70. -(void)showDialog:(NSString *) str 
  71.     
  72.     UIAlertView * alert= [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];     
  73.     
  74.     [alert show];       
  75.     [alert release]; 
  76.  
  77. - (void)viewDidUnload 
  78.     [super viewDidUnload]; 
  79.     // Release any retained subviews of the main view. 
  80.     // e.g. self.myOutlet = nil; 
  81.  
  82. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
  83.     // Return YES for supported orientations 
  84.     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
  85.  
  86. @end 

本文标签

除非注明,文章均为( noway )原创,转载请保留链接: http://blog-old.z3a105.com/?p=532

IOS 入门开发之创建标题栏UINavigationBar的使用:等您坐沙发呢!

发表评论





       ==QQ:122320466==

 微信    QQ群


0

Aujourd’hui, une partie avec le développement du e-commerce, achats en ligne est devenu une partie de la vie pour beaucoup de gens. La mariage a commencé achats en ligne. Si vous choisissez achats les mariages en ligne, il peut être beaucoup moins cher que le salon de la Robe de mariée pas chermariée local, réduisant le budget de mariage. vous pouvez avoir beaucoup de choix si acheter de mariage en ligne. vous pouvez ramasser une robe de mariée bon marché sur Internet.
Piercing fascinerande figur, och nu tittar vi på 2016 senast brudklänning, kan du vara den vackraste bruden det!2016 senaste Bra brudklänning, söt temperament Bra design, romantiska spetsar blomma kjol, som du lägger till en elegant och charmig temperament.Kvinnan tillbaka mjuka linjer, människor brudklänningofta få en känsla av oändlig frestelse. Fall 2016 mässan, lämnar uppgifter om ditt bröllop charmig.
Yesterday afternoon, the Chinese team was training in the Guangzhou Gymnasium, when the reporter asked Zhao Yunlei the feeling of wearing the new cheap jersey , cheap jerseys online shopshe readily took a shirt from the bag crumpled ball to reporters, and she said with a smile: ” This shirt is light. ”Zhao Yunlei said: “Our material is very light like with the clothes of the tennis King Nadal, Federer, after the sweat, sweat does not drip down to the ground, when we do move, it is easy pace slipping if the sweat drip on the floor.”Tennis players Zhang Yawen, told reporters: “You might think the clothes attached to the body, fearing we swing will be affected, in fact, we do not feel anything, because the clothes are very light, very soft, put on quite comfortable. And it’s particularly good clothes to dry, washing and will dry in 15 minutes. ”
China’s sports enthusiasts NFL sweatshirt with mad love and the pursuit of, and therefore, NFL jerseys have a good market in China and development. China is a populous country, is the consumer, the economic momentum is so good, the sales prospects sportswear is immeasurable. With hot sales sweatshirt, but also to promote the importance of sports fans, on health, on the other hand is a matter of concern for the World Cup, fans wearing NFL jerseys and also can express themselves more fully love and obsession Therefore, NFL jerseys Wholesale jerseys online shopwholesale has good prospects and development in China.
ANTA-ANTA Sports Products Limited, referred to as ANTA Sports, Anta, is China’s leading sporting goods companies, mainly engaged in the design, development, manufacture and marketing of ANTA brand sporting goods, including sports footwear, apparel and accessories. Anta sweatshirt design advantages, warm stretch knit fabric, using Slim version of model, more personal fit, bid farewell to bloated, so wearing more stylish.GUIRENNIAO-This logo is a spiritual totem, smooth graphics implication unstoppable force; flexible deliver an elegant arc Wholesale jerseys china shop movement, strength and speed of the United States, a symbol of passion and rationality publicity “Heart” and “meaning”, “concept” unity; pass the fearless and enterprising mind, showing beyond the realm of self, to unstoppable force to create the future.XTEP-Xtep (China) Co., Ltd. is a comprehensive development wholesale jerseys china shop, production and marketing of Xtep brand (XTEP) sports shoes, clothing, bags, caps, balls, socks mainly large sporting goods industry enterprises.
There are a lot of fans in identifying the authenticity of the above cheap jerseys have great distress, so here to i will show you some methods to definitely affordable inexpensive cheap jerseys : Firstly, we should look at if it is working fine. China has been called the world’s factory, a lot cheap jerseys factories in China have foundries, but our cheap jerseys are all from here! Secondly, should to see whether it is the , we all know that it is difficult to get out of print once a genuine cheap cheap jerseys free shipping jersey was print. and we have all kind of stocka on the whole website, in other words, we have all you want ! Finally, look at the price, our price is not necessarily the lowest in the whole website but it must be most fair on the whole website, we certainly you will not regret later when you buy it. Of course, except that cheap jerseys, we also have the other products, such as socks, leggings and some other related products, everyone can enjoy the best services of here!

KUBET