本篇文章給大家分享的是有關(guān)如何在iOS中自定義target和action,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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、導(dǎo)航左側(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與導(dǎo)航效果一致。
關(guān)于UITabBarItem在這里就不多介紹,只是拿其顯示效果與UIBarButtonItem對比。
在開發(fā)過程中,我們會(huì)使用到自定義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)方法不能實(shí)現(xiàn)項(xiàng)目需求效果。當(dāng)然也可以通過屬性保存UIBarButtonItem方法來實(shí)現(xiàn)需求效果。即在點(diǎn)擊按鈕響應(yīng)后,直接使用保存的UIBarButtonItem,但是我沒有采用這種方法。
下面是我給出的兩種解決方案:
方案一
繼承UIBarButtonItem,實(shí)現(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>
以上就是如何在iOS中自定義target和action,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:如何在iOS中自定義target和action-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article30/ehgpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)公司、小程序開發(fā)、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容