欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS自定義UIBarButtonItem的target和action示例代碼

 更新時間:2019年02月22日 08:34:28   作者:平原河流  
這篇文章主要給大家介紹了關(guān)于iOS自定義UIBarButtonItem的target和action的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

需求描述:

在項目開發(fā)過程中,遇到一種情況,需要自定義UIBarButtonItem,來實現(xiàn)分享樣式,并在iPad中彈出系統(tǒng)分享框(UIActivityViewController),系統(tǒng)分享框需要指定顯示位置(barButtonItem)。而自定義的UIBarButtonItem target指向的是UIButton。這與需求不符,需自定義UIBarButtonItem。

在介紹自定義UIBarButtonItem前,先介紹一下相關(guān)控件的子父類關(guān)系(也可以說繼承關(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和UITabBarItem效果顯示

從上圖中看到UIBarButtonItem有三種效果顯示,分別是

1、導航左側(cè)返回按鈕,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,其中包括自定義圖片和系統(tǒng)樣式

- (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對比。

在開發(fā)過程中,我們會使用到自定義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>>

通過上面描述,發(fā)現(xiàn)系統(tǒng)方法不能實現(xiàn)項目需求效果。當然也可以通過屬性保存UIBarButtonItem方法來實現(xiàn)需求效果。即在點擊按鈕響應(yīng)后,直接使用保存的UIBarButtonItem,但是我沒有采用這種方法。

下面是我給出的兩種解決方案:

方案一

繼承UIBarButtonItem,實現(xiàn)子類。

定義子類

#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

定義子類對象,調(diào)用子類對象

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

調(diào)用類別方法

- (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做消息傳遞。

你更喜歡哪一種?!

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • iOS當多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解

    iOS當多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的方法詳解

    在多線程中,有時候我們會遇到一個界面同時有多個網(wǎng)絡(luò)請求(比如a,b,c,d四個網(wǎng)絡(luò)請求),在這四個個請求結(jié)束后,在請求到數(shù)據(jù)去做其他操作(UI更新等),下面這篇文章主要給大家介紹了關(guān)于iOS當多個網(wǎng)絡(luò)請求完成后執(zhí)行下一步的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12
  • iOS中字符串換行的實現(xiàn)方法

    iOS中字符串換行的實現(xiàn)方法

    大家應(yīng)該都有所體會,單行字符數(shù)過多會影響美觀,所以下面這篇文章主要給大家介紹了關(guān)于iOS中字符串換行的實現(xiàn)方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2018-07-07
  • iOS 13適配匯總(推薦)

    iOS 13適配匯總(推薦)

    這篇文章主要介紹了iOS 13適配匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • IOS開發(fā)之路--C語言基礎(chǔ)知識

    IOS開發(fā)之路--C語言基礎(chǔ)知識

    當前移動開發(fā)的趨勢已經(jīng)勢不可擋,這個系列希望淺談一下個人對IOS開發(fā)的一些見解,今天我們從最基礎(chǔ)的C語言開始,C語言部分我將分成幾個章節(jié)去說,今天我們簡單看一下C的一些基礎(chǔ)知識,更高級的內(nèi)容我將放到后面的文章中。
    2014-08-08
  • iOS利用MJRefresh實現(xiàn)自定義刷新動畫效果

    iOS利用MJRefresh實現(xiàn)自定義刷新動畫效果

    本文主要介紹iOS 利用MJRefresh實現(xiàn)自定義動畫的上拉刷新下拉加載效果,要想實現(xiàn)此功能,首先得有一套load的圖片數(shù)組。接下來通過本文給大家詳解介紹實現(xiàn)過程
    2017-02-02
  • iOS中PNChart與UITableView的聯(lián)動示例詳解

    iOS中PNChart與UITableView的聯(lián)動示例詳解

    PNChart是個界面很漂亮的圖表第三方庫,UITableView則不用過多介紹了,各位iOS開發(fā)者們都知道,下面這篇文章主要給大家介紹了關(guān)于iOS中PNChart與UITableView的聯(lián)動的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • iOS登錄時驗證手機號與倒計時發(fā)送驗證碼問題詳解

    iOS登錄時驗證手機號與倒計時發(fā)送驗證碼問題詳解

    這篇文章主要給大家介紹了關(guān)于iOS登錄時驗證手機號與倒計時發(fā)送驗證碼問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2019-01-01
  • iOS實現(xiàn)截取字符串中漢字功能

    iOS實現(xiàn)截取字符串中漢字功能

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)截取字符串中漢字功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS支付寶支付方法詳解

    iOS支付寶支付方法詳解

    這篇文章主要為大家詳細介紹了iOS支付寶支付方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • IOS開發(fā)實現(xiàn)錄音功能

    IOS開發(fā)實現(xiàn)錄音功能

    本文給大家分享的是一個IOS開發(fā)中實現(xiàn)錄音功能的實例,并簡單給大家解析一下,有需要的小伙伴可以參考下
    2016-03-03

最新評論