千家信息网

iOS如何自定义UIBarButtonItem的target和action

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,这篇文章主要介绍了iOS如何自定义UIBarButtonItem的target和action,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一
千家信息网最后更新 2025年02月05日iOS如何自定义UIBarButtonItem的target和action

这篇文章主要介绍了iOS如何自定义UIBarButtonItem的target和action,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

需求描述:

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

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

1、UIBarItem

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarItem : NSObject

2、UIBarButtonItem

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIBarButtonItem : UIBarItem

3、UITabBarItem

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem

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:>

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

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

方案一

继承UIBarButtonItem,实现子类。

定义子类

#import @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:

方案二

UIBarButtonItem类别

定义类别

#import @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:

感谢你能够认真阅读完这篇文章,希望小编分享的"iOS如何自定义UIBarButtonItem的target和action"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

效果 方法 子类 对象 篇文章 系统 需求 按钮 方案 类别 图片 属性 样式 过程 项目 导航 开发 一致 价值 位置 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 金塔网络安全宣传周 国外开源软件开发平台 徐州秀英星网络技术有限公司 关于小班网络安全教育图片 增城正规网络安全运维 关于网络安全个人对照检查 21世纪 数据库 java多数据源数据库管理 mysql数据库无法登陆 医疗卫生行业网络安全执法检查 网络安全法对校园欺凌的处理 杰发科技软件开发笔试题 队列怎么存入数据库 企业文件管理服务器搭建 双线服务器的配置 文献信息数据库的基本单位不包括 geo数据库找某个基因表达量 四川服务器阵列卡品牌 软件开发验收要达到哪些要求 集成计算服务器生产厂家 无服务器怎么重新登录设置 静安区企业软件开发报价有哪些 云服务器有哪些 网吧系统服务器怎么做的 服务器供电模块 亲爱的热爱的为什么是网络安全 驻马店源话网络技术有限公司 维普数据库检索输出包括 博图创建tcp服务器 无法建立到服务器的安全链接
0