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

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

 http://xys289187120.blog.51cto.com/3361352/685746

 

    IOS 开发有关界面的东西不仅可以使用代码来编写,也可以使用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.      
  4.     [self showDialog:@"点击了导航栏左边按钮"]; 
  5.    
  6. } 
  7.  
  8.  
  9. -(void)clickRightButton 
  10. { 
  11.      
  12.     [self showDialog:@"点击了导航栏右边按钮"]; 
  13.      
  14. } 

 

 




点击后打开一个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. } 




最后贴上完整的代码

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