项目里的通用标签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];
}
运行的结果如下:当我没做任何操作时右上角按钮不可用:
当我点击开始按钮的时候,右上角的按钮就可用了
再接着点击右上角的按钮时就可以对这个控制器里进行其他的任何操作。比如,我这个例子里面是改变这个开始按钮的背景颜色和字体颜色。