iOS应用开发中导航栏按钮UIBarButtonItem的添加教程

所属分类: 软件编程 / IOS 阅读数: 1146
收藏 0 赞 0 分享

1、UINavigationController导航控制器如何使用
UINavigationController可以翻译为导航控制器,在iOS里经常用到。
我们看看它的如何使用:
下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制

20162494526157.png (636×288)

2、UINavigationController的结构组成
看下图,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等组成。

20162494548655.png (646×476)

现在我们建立一个例子,看看如何使用UINavigationController
3、新建一个项目
命名为UINavigationControllerDemo,为了更好理解UINavigationController,我们选择Empty Application模板

20162494608179.png (728×491)

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

20162494625133.png (728×491)

选择正确位置创建完成,这时项目里多了三个文件,分别是RootViewController.h RootViewController.m RootViewController.xib文件。
打开RootViewController.xib,添加一个按钮控件,按钮Button改成 :Goto SecondView,为跳转做准备

20162494706858.png (923×284)

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

复制代码 代码如下:

@property (strong, nonatomic) UINavigationController *navController; 

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

#import <UIKit/UIKit.h> 
 
@class ViewController; 
 
@interface AppDelegate : UIResponder <UIApplicationDelegate> 
 
@property (strong, nonatomic) UIWindow *window; 
 
@property (strong, nonatomic) ViewController *viewController; 
 
@property (strong, nonatomic) UINavigationController *navController; 
 
@end 

6、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中创建添加navController,RootViewController视图。
复制代码 代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    RootViewController *rootView = [[RootViewController alloc] init]; 
    rootView.title = @"Root View"; 
     
    self.navController = [[UINavigationController alloc] init]; 
    [self.navController pushViewController:rootView animated:YES]; 
    [self.window addSubview:self.navController.view]; 
    [self.window makeKeyAndVisible]; 
    return YES; 


给rootView的titie命名为 Root View,好识别View直接的切换关系。用pushViewController把rootView加入到navController的视图栈中。
7、现在Root视图添加完成
看看效果:

20162494734438.png (368×716)

现在还没有Navigation bar 。只有title。
8、添加UIBarButtonItem
bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。
在RootViewController.m中添加代码如下:

复制代码 代码如下:

- (void)viewDidLoad 

    [super viewDidLoad]; 
 
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)]; 
    self.navigationItem.leftBarButtonItem = leftButton; 
     
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)]; 
    self.navigationItem.rightBarButtonItem = rightButton;<p class="p1">}

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

20162494751281.png (362×214)

这里重点介绍下

复制代码 代码如下:

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


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

20162494809920.png (692×640)

9、响应UIBarButtonItem的事件的实现
我们在 action:@selector(selectLeftAction:);
action添加了selectLeftAction和selectRightAction
在RootViewController.m文件中添加代码实现:

复制代码 代码如下:

-(void)selectLeftAction:(id)sender 

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 
    [alter show]; 

 
-(void)selectRightAction:(id)sender 

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏右按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; 
    [alter show]; 
}
 
这样在点击左右的UIBarButtonItem时,弹出提示:

20162494825463.png (281×302)


两个按钮切换的简单例子

下面这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add button。代码实现的按钮切换/隐藏功能具体就是:点击 edti button 的话,back button 隐藏,同时显示 add button。用户编辑完以后则显示 back button 隐藏 add button。这一功能在很多应用里都会用到,而且适当隐藏掉无用按钮对保持界面简洁以及引导用户操作都是有意义的。

复制代码 代码如下:

- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
 
    [super setEditing:editing animated:animated];
 
// Don't show the Back button while editing.
[self.navigationItem setHidesBackButton:editing animated:YES];
 
if (editing) {
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertMe)] autorelease];
}else {
    self.navigationItem.leftBarButtonItem = nil;
//self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(backButton) ] autorelease];
 }
}

更多精彩内容其他人还在看

iOS开发实现HTTPS之cer文件的使用详解

下面小编就为大家分享一篇iOS开发实现HTTPS之cer文件的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

ios 流媒体播放器实现流程及FreeStreamer的使用的示例

本篇文章主要介绍了ios 流媒体播放器实现流程及FreeStreamer的使用的示例代码,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS NSURLSessionDownloadTask实现文件断点下载的方法

本篇文章主要介绍了iOS NSURLSessionDownloadTask实现文件断点下载的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS NSURLSessionDownloadTask设置代理文件下载的示例

本篇文章主要介绍了iOS NSURLSessionDownloadTask设置代理文件下载的示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

浅谈iOS UIWebView对H5的缓存功能

本篇文章主要介绍了浅谈iOS UIWebView对H5的缓存功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS中UITextField实现过滤选中状态拼音的代码

这篇文章主要介绍了iOS中UITextField实现过滤选中状态拼音的代码,需要的朋友可以参考下
收藏 0 赞 0 分享

简单谈谈c/c++中#import、#include和@class的区别

对于#import,我想做过iOS开发的人应该都不陌生。在开发过程中,当我们需要声明某一个类时,都需要去引用。而#imclude的话,在我们学习C时就已经知道了,他的作用也是引用声明的意思。在表面上他们的作用似乎都是一样的。但是在具体功能实现方式上,还是有着很大的区别。
收藏 0 赞 0 分享

Objective-C中的语法糖示例详解

开发过程中我特别喜欢用语法糖,原因很简单,懒得看到一堆长长的代码,但语法糖简单却不那么简单,下面这篇文章主要给大家介绍了关于Objective-C中语法糖的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS弹幕开发中遇到的问题汇总

最近做项目的时候需要实现弹幕这个功能, 虽然感觉实现起来也不是很复杂,但还是遇到了一些问题,下面这篇文章主要给大家总结介绍了在iOS弹幕开发中遇到的问题,文中将解决的方法介绍的非常详细,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS常用组件之高效切圆角的方法汇总

最近在研究切圆角的方法,也找了下网上的资料,所以下面这篇文章主要给大家总结介绍了关于iOS常用组件之高效切圆角的一些方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享
查看更多