- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- iOS如何自定義UIBarButtonItem的target和action
這篇文章主要介紹了iOS如何自定義UIBarButtonItem的target和action,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
需求描述:
在項目開(kāi)發(fā)過(guò)程中,遇到一種情況,需要自定義UIBarButtonItem,來(lái)實(shí)現分享樣式,并在iPad中彈出系統分享框(UIActivityViewController),系統分享框需要指定顯示位置(barButtonItem)。而自定義的UIBarButtonItem target指向的是UIButton。這與需求不符,需自定義UIBarButtonItem。
在介紹自定義UIBarButtonItem前,先介紹一下相關(guān)控件的子父類(lèi)關(guān)系(也可以說(shuō)繼承關(guān)系)。
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有三種效果顯示,分別是
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與導航效果一致。
關(guān)于UITabBarItem在這里就不多介紹,只是拿其顯示效果與UIBarButtonItem對比。
在開(kāi)發(fā)過(guò)程中,我們會(huì )使用到自定義UIBarButtonItem,來(lái)顯示我們想要的界面效果。使用的方法常為:
- (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,其類(lèi)型是UIButton。
2017-10-17 16:08:43.917 TestImage[5482:163865] sender:<UIButton: 0x7fb9bad12e60; frame = (0 0; 60 40); opaque = NO; layer = <CALayer: 0x61000003b940>>
通過(guò)上面描述,發(fā)現系統方法不能實(shí)現項目需求效果。當然也可以通過(guò)屬性保存UIBarButtonItem方法來(lái)實(shí)現需求效果。即在點(diǎn)擊按鈕響應后,直接使用保存的UIBarButtonItem,但是我沒(méi)有采用這種方法。
下面是我給出的兩種解決方案:
方案一
繼承UIBarButtonItem,實(shí)現子類(lèi)。
定義子類(lèi)
#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
定義子類(lèi)對象,調用子類(lèi)對象
- (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類(lèi)別
定義類(lèi)別
#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
調用類(lèi)別方法
- (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>
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站