需求描述:

在项目开发过程中,遇到一种情况,需要自定义UIBarButtonItem,来实现分享样式,并在iPad中弹出系统分享框(UIActivityViewController),系统分享框需要指定显示位置(barButtonItem)。而自定义的UIBarButtonItem target指向的是UIButton。这与需求不符,需自定义UIBarButtonItem。

在介绍自定义UIBarButtonItem前,先介绍一下相关控件的子父类关系(也可以说继承关系)。

1、UIBarItem

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject <NSCoding, UIAppearance>

2、UIBarButtonItem

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarButtonItem : UIBarItem <NSCoding>

3、UITabBarItem

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem

下面是在界面上的显示效果

UIBarButtonItem和UITabBarItem效果显示

从上图中看到UIBarButtonItem有三种效果显示,分别是

1、导航左侧返回按钮,UINavigationItem中的backBarButtonItem属性

@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem

2、纯文本的UIBarButtonItem

- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;

3、纯图片的UIBarButtonItem,其中包括自定义图片和系统样式

- (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;

UIToolBar使用UIBarButtonItem与导航效果一致。

关于UITabBarItem在这里就不多介绍,只是拿其显示效果与UIBarButtonItem对比。

在开发过程中,我们会使用到自定义UIBarButtonItem,来显示我们想要的界面效果。使用的方法常为:

- (instancetype)initWithCustomView:(UIView *)customView;
- (void)viewDidLoad {
 [super viewDidLoad];
 //自定义View
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
 view.backgroundColor = [UIColor redColor];
 //自定义按钮
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 btn.frame = view.bounds;
 [btn addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
 [view addSubview:btn];
 //自定义Item
 UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:view];
 //
 self.navigationItem.leftBarButtonItem = barItem;
}

#pragma mark -

- (void)clickRight:(id)sender {
 NSLog(@"sender:%@",sender);
}

其中打印sender,其类型是UIButton。

2017-10-17 16:08:43.917 TestImage[5482:163865] sender:<UIButton: 0x7fb9bad12e60; frame = (0 0; 60 40); opaque = NO; layer = <CALayer: 0x61000003b940>>

通过上面描述,发现系统方法不能实现项目需求效果。当然也可以通过属性保存UIBarButtonItem方法来实现需求效果。即在点击按钮响应后,直接使用保存的UIBarButtonItem,但是我没有采用这种方法。

下面是我给出的两种解决方案:

方案一

继承UIBarButtonItem,实现子类。

定义子类

#import <UIKit/UIKit.h>

@interface LLBarButtonItem : UIBarButtonItem


@end
#import "LLBarButtonItem.h"

@implementation LLBarButtonItem

- (id)initWithCustomView:(UIView *)customView {
 self = [super initWithCustomView:customView];
 if (self) {
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 btn.frame = customView.bounds;
 btn.backgroundColor = [UIColor clearColor];
 [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
 [customView addSubview:btn];
 }
 return self;
}

- (void)clickButton:(UIButton *)sender {
 if (self.target && [self.target respondsToSelector:self.action]) {
 //[self.target performSelector:self.action withObject:self];
 IMP imp = [self.target methodForSelector:self.action];
 
 void (*func)(id, SEL, id) = (void *)imp;
 
 func(self.target, self.action, self);
 }
}

@end

定义子类对象,调用子类对象

- (void)viewDidLoad {
 [super viewDidLoad];
 //自定义View
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
 view.backgroundColor = [UIColor clearColor];
 //自定义Item
 LLBarButtonItem *barItem = [[LLBarButtonItem alloc] initWithCustomView:view];
 barItem.target = self;
 barItem.action = @selector(clickRight:);
 //
 self.navigationItem.leftBarButtonItem = barItem;
}

#pragma mark -

- (void)clickRight:(id)sender {
 NSLog(@"sender:%@",sender);
}

打印target对象

2017-10-17 16:24:11.696 TestImage[5557:170144] sender:<LLBarButtonItem: 0x7fb403c16080>

方案二

UIBarButtonItem类别

定义类别

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (Custom)

- (void)addCutomTarget:(id)target action:(SEL)action;

@end
#import "UIBarButtonItem Custom.h"

@implementation UIBarButtonItem (Custom)

- (void)addCutomTarget:(id)target action:(SEL)action {
 if (self.customView != nil) {
 self.target = target;
 self.action = action;
 //
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 btn.frame = self.customView.bounds;
 btn.backgroundColor = [UIColor clearColor];
 [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
 [self.customView addSubview:btn];
 }
}

- (void)clickButton:(UIButton *)sender {
 if (self.target && [self.target respondsToSelector:self.action]) {
 //[self.target performSelector:self.action withObject:self];
 IMP imp = [self.target methodForSelector:self.action];
 
 void (*func)(id, SEL, id) = (void *)imp;
 
 func(self.target, self.action, self);
 }
}

@end

调用类别方法

- (void)viewDidLoad {
 [super viewDidLoad];
 //自定义View
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
 view.backgroundColor = [UIColor clearColor];
 //自定义Item
 UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:view];
 [barItem addCutomTarget:self action:@selector(clickRight:)];
 //
 self.navigationItem.leftBarButtonItem = barItem;
}

#pragma mark -

- (void)clickRight:(id)sender {
 NSLog(@"sender:%@",sender);
}

打印target对象

2017-10-17 16:28:14.407 TestImage[5598:172418] sender:<UIBarButtonItem: 0x7ffeda609e20>

两种方法都使用了IMP做消息传递。

你更喜欢哪一种?!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对Devmax的支持。

iOS自定义UIBarButtonItem的target和action示例代码的更多相关文章

  1. Html5 滚动穿透的方法

    这篇文章主要介绍了Html5 滚动穿透的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. HTML5 拖放(Drag 和 Drop)详解与实例代码

    本篇文章主要介绍了HTML5 拖放(Drag 和 Drop)详解与实例代码,具有一定的参考价值,有兴趣的可以了解一下

  3. 跨域修改iframe页面内容详解

    这篇文章主要介绍了跨域修改iframe页面内容详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  4. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  5. ios – 暂停调度队列是否会暂停其目标队列?

    我想创建两个串行队列A&B.队列B是队列A的目标.我想在B上排队一些块,并暂停它直到我准备执行它们,但是我想继续在队列A上执行块.如果我暂停B,这还会暂停它的目标队列(队列A)吗?我的想法是,我想安排这些特定的块在稍后日期执行但是我不希望它们同时运行而我不这样做想要处理信号量.但我希望队列A继续处理它的块,而B则被暂停如果不清楚这里是一些示例代码解决方法queueB被挂起,但queueA未被挂起.queueA和queueB被挂起.

  6. ios – 使用CocoaPods post install hook将自定义路径添加到HEADER_SEARCH_PATHS

    解决方法在Podfile中定义一个方法:然后在post_install中调用该方法:

  7. ios – 在Swift中删除WKWebView Accesory栏

    我现在正试着将this转换成Swift而没有真正的背景.这是我到目前为止所得到的…而且我一直在寻找谷歌并不知道要搜索什么才能更具体.你能否详细说明我做错了什么?

  8. iOS,ld:framework没有找到适用于架构arm64的GoogleMaps

    Podfile看起来像这个Cocoapodsv1.0beta6):解决方法更新请检查您是否在架构中具有相同的构建设置,并仅构建活动体系结构中的目标键你的podfile应该是这样的在您启动ProjectTest目标之前结束项目目标,也是为什么添加继承!

  9. ios – 为CocoaPods的pod设置部署目标

    我使用CocoaPods来管理项目中的依赖关系.我写了Podfile:此文件与CocoaPods0.x配合使用,但在我更新到CocoaPods1.0之后,我无法编译项目.运行后我无法编译我的项目错误:/Users/

  10. ios – 如何在故事板上单击UIImageView(swift)

    我是新来的,我想知道如何在故事板上单击ImageView.我想要做的是使其点击时,它显示另一个视图控制器.解决方法您可以添加tapGesture.这是代码:Swift3.0

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部