我寫了一個自定義的一個繼承與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"];
}
當(dāng)我在控制器里用到的時候
#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 ---右側(cè)保存標(biāo)簽---
-(void)handRightButton:(UIButton*)sender{
self.beginButton.backgroundColor= [UIColorlightGrayColor];
[self.beginButtonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
}
運(yùn)行的結(jié)果如下:當(dāng)我沒做任何操作時右上角按鈕不可用:
當(dāng)我點(diǎn)擊開始按鈕的時候,右上角的按鈕就可用了
再接著點(diǎn)擊右上角的按鈕時就可以對這個控制器里進(jìn)行其他的任何操作。比如,我這個例子里面是改變這個開始按鈕的背景顏色和字體顏色。