• IOS导航栏的使用方法


    本文是使用纯代码实现一个导航栏的效果。单击按钮并且产生事件。基本思路是:

    1.创建一个导航栏(UINavigationBar对象)

    2.创建一个导航栏集合(UINavigationItem对象)

    3.创建一个左边按钮、一个右边按钮(UIBarButtonItem对象),并实现对应的事件方法

    4.将导航栏集合添加到导航栏中,设置动画关闭

    5.把左右两个按钮添加到导航栏集合中去

    6.在视图中显示当前创建的导航栏

     

    具体的实现代码如下:

    ViewController.h文件中的代码不用改变,如下所示:

    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface ViewController : UIViewController  
    4.   
    5. @end  

    ViewController.m文件中的代码:

    1. #import "ViewController.h"  
    2.   
    3. @interface ViewController ()  
    4.   
    5. @end  
    6.   
    7. @implementation ViewController  
    8.   
    9. - (void)viewDidLoad  
    10. {  
    11.     [super viewDidLoad];  
    12.     // Do any additional setup after loading the view, typically from a nib.  
    13.       
    14.     //创建一个导航栏  
    15.     UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
    16.     //创建一个导航栏集合  
    17.     UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];  
    18.     //在这个集合Item中添加标题,按钮  
    19.     //style:设置按钮的风格,一共有三种选择  
    20.     //action:@selector:设置按钮的点击事件  
    21.     //创建一个左边按钮  
    22.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStyleBordered target:self action:@selector(clickLeftButton)];  
    23.     //创建一个右边按钮  
    24.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];  
    25.       
    26.     //设置导航栏的内容  
    27.     [navItem setTitle:@"凌凌漆"];  
    28.       
    29.     //把导航栏集合添加到导航栏中,设置动画关闭  
    30.     [navBar pushNavigationItem:navItem animated:NO];  
    31.       
    32.     //把左右两个按钮添加到导航栏集合中去  
    33.     [navItem setLeftBarButtonItem:leftButton];  
    34.     [navItem setRightBarButtonItem:rightButton];  
    35.       
    36.     //将标题栏中的内容全部添加到主视图当中  
    37.     [self.view addSubview:navBar];  
    38.       
    39.     //最后将控件在内存中释放掉,以避免内存泄露  
    40.     [navItem release];  
    41.     [leftButton release];  
    42.     [rightButton release];  
    43. }  
    44.   
    45. -(void)showDialog:(NSString *)str  
    46. {  
    47.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];  
    48.     [alert show];  
    49.     [alert release];  
    50. }  
    51.   
    52. -(void) clickLeftButton  
    53. {  
    54.     [self showDialog:@"点击了导航栏左边按钮"];  
    55. }  
    56.   
    57. -(void) clickRightButton  
    58. {  
    59.     [self showDialog:@"点击了导航栏右边按钮"];  
    60. }  
    61.   
    62. - (void)viewDidUnload  
    63. {  
    64.     [super viewDidUnload];  
    65.     // Release any retained subviews of the main view.  
    66. }  
    67.   
    68. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
    69. {  
    70.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
    71. }  
    72.   
    73. @end  
  • 相关阅读:
    linux创建用户
    Java理解笔记------杂项
    java高效并发
    GPG备份秘钥
    (二)数据同步利器syncthing
    (一)安装samba
    (序)利旧打造私有云
    mysql的docker化安装
    (八)netty的SSL renegotiation攻击漏洞
    (七)json序列化
  • 原文地址:https://www.cnblogs.com/Peak-Banish/p/3947975.html
Copyright © 2020-2023  润新知