• IOS学习之UINavigationController详解与使用:添加UIBarButtonItem


    1、UINavigationController导航控制器如何使用

    UINavigationController可以翻译为导航控制器,在IOS里经常用到。

    我们看看它的如何使用:

    下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制器弹出堆栈。

    上图来自苹果官网。

    2、UINavigationController的结构组成

    看下图,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等组成。

    现在我们建立一个例子,看看如何使用UINavigationController

    3、新建一个项目

    命名为UINavigationControllerDemo,为了更好理解UINavigationController,我们选择Empty Application模板

    4、创建一个View Controller,命名为RootViewController:依次选择File——New——New File,默认勾上With XIB for user interface.

    选择正确位置创建完成,这时项目里多了三个文件,分别是RootViewController.h RootViewController.m RootViewController.xib文件。

    打开RootViewController.xib,添加一个按钮控件,按钮Button改成 :Goto SecondView,为跳转做准备

    5、打开AppDelegate.h,向其中添加属性:

    [cpp] view plaincopy
     
    1. @property (strong, nonatomic) UINavigationController *navController;  


    添加后AppDelegate.h文件代码如下:

    [cpp] view plaincopy
     
    1. #import   
    2.   
    3. @class ViewController;  
    4.   
    5. @interface AppDelegate : UIResponder   
    6.   
    7. @property (strong, nonatomic) UIWindow *window;  
    8.   
    9. @property (strong, nonatomic) ViewController *viewController;  
    10.   
    11. @property (strong, nonatomic) UINavigationController *navController;  
    12.   
    13. @end  


    6、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中创建添加navController,RootViewController视图。

    [cpp] view plaincopy
     
    1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    2. {  
    3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    4.     RootViewController *rootView = [[RootViewController alloc] init];  
    5.     rootView.title = @"Root View";  
    6.       
    7.     self.navController = [[UINavigationController alloc] init];  
    8.     [self.navController pushViewController:rootView animated:YES];  
    9.     [self.window addSubview:self.navController.view];  
    10.     [self.window makeKeyAndVisible];  
    11.     return YES;  
    12. }  

    给rootView的titie命名为 Root View,好识别View直接的切换关系。用pushViewController把rootView加入到navController的视图栈中。

    7、现在Root视图添加完成

    看看效果:

    '

    现在还没有Navigation bar 。只有title。

    8、添加UIBarButtonItem

    bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。

    在RootViewController.m中添加代码如下:

    [cpp] view plaincopy
     
    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.   
    5.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];  
    6.     self.navigationItem.leftBarButtonItem = leftButton;  
    7.       
    8.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)];  
    9.     self.navigationItem.rightBarButtonItem = rightButton;

      }

        

    这样添加了UIBarButtonItem了,效果如下:

    这里重点介绍下

    UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];

    UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,看下图,你不用一个个试验,你也知道想用那个item,如下图:

    9、响应UIBarButtonItem的事件的实现

    我们在 action:@selector(selectLeftAction:);

    action添加了selectLeftAction和selectRightAction

    在RootViewController.m文件中添加代码实现:

    [cpp] view plaincopy
     
    1. -(void)selectLeftAction:(id)sender  
    2. {  
    3.     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
    4.     [alter show];  
    5. }  
    6.   
    7. -(void)selectRightAction:(id)sender  
    8. {  
    9.     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏右按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
    10.     [alter show];  
    11. }  

    这样在点击左右的UIBarButtonItem时,弹出提示:

  • 相关阅读:
    AI工程师职业规划和学习路线完整版
    Python基础面试题库
    Python运行的17个时新手常见错误小结
    一文总结学习 Python 的 14 张思维导图
    NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码)
    超过 150 个最佳机器学习,NLP 和 Python教程
    任泽平:95页PPT分析2018(经济、房价、政策)
    为什么量化交易中稳定盈利是第一要义
    使用tushare获取股票实时分笔数据延时有多大
    5行代码实现1秒内获取一次所有股票的实时分笔数据
  • 原文地址:https://www.cnblogs.com/Liaoyang/p/3354914.html
Copyright © 2020-2023  润新知