項目里的通用標簽UInavigationItem通用標簽

我寫了一個自定義的一個繼承與UIBarButtonItem的類(這個主要用到的就是KVO)

@protocol RightButtonDelegate

-(void)handRightButton:(UIButton *)sender;

@end

@interfaceRightButton : UIBarButtonItem

-(UIBarButtonItem *)initWithImagebeginImage:(UIImage *)image withLightImage:(UIImage *)lightImage;

-(void)changeEdit:(BOOL)isEdit;

@property(nonatomic,strong)id?delegate;

@end





.m文件

#import"RightButton.h"

@interfaceRightButton(){

}

@property(nonatomic,strong)UIImage *darkImage;

@property(nonatomic,strong)UIImage *lightImage;

@property(nonatomic,strong)UIButton *button;

@property(nonatomic,assign)BOOLisEdit;

@end

@implementationRightButton

-(instancetype)init{

if(self= [superinit]) {

}

returnself;

}

-(UIButton *)button{

if(!_button) {

_button = [[UIButton alloc]init];

_button.frame = CGRectMake(0,0,50,30);

_button.enabled =NO;

[_button addTarget:selfaction:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];

}

return_button;

}

-(void)saveClick:(UIButton *)button{

if(self.delegate &&[self.delegate respondsToSelector:@selector(handRightButton:)]) {

[self.delegate handRightButton:button];

}

}

-(UIBarButtonItem *)initWithImagebeginImage:(UIImage *)image withLightImage:(UIImage *)lightImage{

[selfaddsuBViewS];

self.darkImage = image;

self.lightImage = lightImage;

self.button.enabled =NO;

[self.button setImage:image forState:UIControlStateNormal];

[selfaddObserver:selfforKeyPath:@"isEdit"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

returnself;

}

-(void)changeEdit:(BOOL)isEdit{

self.isEdit = isEdit;

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{

[self.button setImage:self.lightImage forState:UIControlStateNormal];

self.button.enabled =YES;

}

-(void)addsuBViewS{

self.customView =self.button;

}

-(void)dealloc{

[selfremoveObserver:selfforKeyPath:@"isEdit"];

}



當我在控制器里用到的時候

#import"RightButton.h"

@interface ViewController()

@property(nonatomic,strong)RightButton*rightButton;

@property(nonatomic,strong)UIButton*beginButton;//開始按鈕

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor= [UIColorwhiteColor];

[self uiconfig];

[self start];

// Do any additional setup after loading the view, typically from a nib.

}

#pragma mark ---開始按鈕---

-(void)start{

NSArray*arr =@[@"開始"];

for(inti =0; i < arr.count; i++) {

UIButton*button = [[UIButtonalloc]init];

button.frame=CGRectMake(50,64,100,30);

[buttonsetTitle:arr[i]forState:UIControlStateNormal];

[buttonsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];

button.backgroundColor= [UIColorredColor];

[buttonaddTarget:selfaction:@selector(begin:)forControlEvents:UIControlEventTouchUpInside];

self.beginButton= button;

[self.viewaddSubview:button];

}

}

-(void)begin:(UIButton*)button{

[_rightButtonchangeEdit:YES];

}

-(void)uiconfig{

UIImage*image = [[UIImageimageNamed:@"studentLightSave"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage*rightImage = [[UIImageimageNamed:@"studentSave"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

_rightButton= [[RightButtonalloc]initWithImagebeginImage:imagewithLightImage:rightImage];

self.navigationItem.rightBarButtonItem=_rightButton;

_rightButton.delegate=self;

}

#pragma mark ---右側保存標簽---

-(void)handRightButton:(UIButton*)sender{

self.beginButton.backgroundColor= [UIColorlightGrayColor];

[self.beginButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

}

運行的結果如下:當我沒做任何操作時右上角按鈕不可用:

當我點擊開始按鈕的時候,右上角的按鈕就可用了


再接著點擊右上角的按鈕時就可以對這個控制器里進行其他的任何操作。比如,我這個例子里面是改變這個開始按鈕的背景顏色和字體顏色。


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容