欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS自定义UIBarButtonItem的target和action示例代码

程序员文章站 2023-11-25 12:53:10
需求描述: 在项目开发过程中,遇到一种情况,需要自定义uibarbuttonitem,来实现分享样式,并在ipad中弹出系统分享框(uiactivityviewcontr...

需求描述:

在项目开发过程中,遇到一种情况,需要自定义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

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

iOS自定义UIBarButtonItem的target和action示例代码

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做消息传递。

你更喜欢哪一种?!

总结

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