[[UIBarButtonItem alloc] initWithTitle与[[UIBarButtonItem alloc] initWithBarButtonSystemItem的区别

来源:互联网 发布:linux killproc 编辑:程序博客网 时间:2024/06/05 09:24

总结的区别是:[[UIBarButtonItem alloc] initWithTitle主要用于定义按钮的文字标题,[[UIBarButtonItem alloc] initWithBarButtonSystemItem主要用于定义按钮的图片(不能定义标题)。


我们看下这两种初始化方法在苹果开发文档中是如何定义的:

一、

[[UIBarButtonItem alloc] initWithTitle:style:target:action

从字面理解可知,initWithTitle:style:target:action接口用于定义UIBarButtonItem的标题(title)、按钮样式(只能使用UIBarButtonItemStyle枚举中定义的)与点击事件

---------------------------------------------------------------

从上方文档截图可以看出,按钮样式定义在UIBarButtonItemStyle枚举中。我们来看看苹果开发文档如何定义UIBarButtonItemStyle枚举:


可见只有三种样式可选。它们在外观上的区别如下(摘自:http://dev.son1c.com/show/2596.html):

(UIBarButtonItemStylePlain,默认按钮风格)                   

(UIBarButtonItemStyleBordered)                                    

(UIBarButtonItemStyleDone)                                             

三种样式在表示意义上的区别(参考自:http://book.51cto.com/art/201003/186427.htm):

UIBarButtonItemStylePlain - 按下时会闪动  (这个需要考证,因为本人目前没有iPhone手机可以测试)

UIBarButtonItemStyleBordered - 与UIBarButtonItemStylePlain相同,但显示的按钮有边框  (经测试,这个观点是错误的——把两个按钮截图放大,用PhotoShop的吸管工具吸取两个按钮对应位置的像素,发现颜色值完全相同)

UIBarButtonItemStyleDone - 提醒用户编辑完毕时应该点触(tap)该按钮


==========================================================


二、

[[UIBarButtonItem alloc] initWithBarButtonSystemItem:target:action


从字面理解可知,initWithBarButtonSystemItem:target:action:接口用于定义UIBarButtonItem的图片内容(只能使用UIBarButtonSytemItem枚举中定义的)与点击事件。

从上方文档截图可以看出,按钮样式定义在UIBarButtonItemItem枚举中。我们来看看苹果开发文档如何定义UIBarButtonItemStyle枚举:


从外观来说明每个枚举值(摘自:http://dev.son1c.com/show/2596.html):

                   标签      效果                       标签         效果UIBarButtonSystemItemAction            UIBarButtonSystemItemPause        UIBarButtonSystemItemAdd            UIBarButtonSystemItemPlay        UIBarButtonSystemItemBookmarks            UIBarButtonSystemItemRedo        UIBarButtonSystemItemCamera            UIBarButtonSystemItemRefresh        UIBarButtonSystemItemCancel            UIBarButtonSystemItemReply        UIBarButtonSystemItemCompose            UIBarButtonSystemItemRewind        UIBarButtonSystemItemDone            UIBarButtonSystemItemSave        UIBarButtonSystemItemEdit            UIBarButtonSystemItemSearch        UIBarButtonSystemItemFastForward            UIBarButtonSystemItemStop        UIBarButtonSystemItemOrganize            UIBarButtonSystemItemTrash        UIBarButtonSystemItemPageCurl            UIBarButtonSystemItemUndo        

==========================================================


要注意,[[UIBarButtonItem alloc] initWithTitle与[[UIBarButtonItem alloc] initWithBarButtonSystemItem的区别还在于:

一个UIBarButtonItem,如果此按钮是赋值给self.navigationItem.backBarButtonItem,也就是说定义了一个返回按钮,那么使用[[UIBarButtonItem alloc] initWithTitle才能看到自定义的效果(虽然两种初始化方式在编译时都能通过),并且只有title参数生效,外观依然为系统默认的返回按钮。


==========================================================

关于返回按钮(self.navigationItem.backBarButtonItem):


无论使用哪个初始化方法,定义的返回按钮将显示在“下一个视图”,而不是当前视图

系统只允许用户获取下一个视图的UIBackBarButtonItem,不允许获取当前视图的UIBackBarButtonItem(无论处于哪个视图都是这种说法)

self.navigationItem.backBarButtonItem的默认值为nil,除非用户对其赋值,否则无论在当前视图控制器(ViewController)的哪个生命周期中,self.navigationItem.backBarButtonItem永远为nil;

③下一个视图B的返回按钮由全局的NavigationController负责生成(它设置ViewController.navigationItem.backBarButtonItem属性),生成时机(依本人理解)为当前视图控制器A已经从NavigationController出栈而视图控制器B入栈时,此时视图控制器B正处于初始化阶段,控制器包含的View尚未显示在界面);

由全局的NavigationController负责生成的证据:苹果开发文档搜索关键词“UINavigationController”,可以看到里面写道:

In addition,the navigation controller object builds the contents of the navigation bar dynamically using the navigation items(instances of the UINavigationItem class)associated with the view controllers on the navigation stack.

④系统只允许用户定义返回UIBackBarButtonItem(也即是下个视图的返回按钮)的标题(title)和背景


定义背景的方法,提供两个本人觉得有参考价值的网页(未经本人验证正确性):

自定义iOS7导航栏背景,标题和返回按钮文字颜色

使用图片方式自定义iOS导航栏navigationItem的backBarButtonItem

0 0
原创粉丝点击