控件功能:
在View上嵌一个控件,从服务器获得defaultNumber,在页面加载后默认显示,然后按"+",个数增加;按"-",个数减少。在个数小于minNumber时,"-"按钮不可用。
控件实现
新建一个类numberSelectView,继承UIView。控件都是在xib文件中使用,需要实现-(id)initWithCoder方法进行初始化。调用类和numberSelectView类的点击事件的关联,用协议和代理实现。
代码
代码最直接了
.h文件
#import <UIKit/UIKit.h>
@class NumberSelectView;
@protocol NumberSelectViewDelegate <NSObject>
-(void)numberSelectView:(NumberSelectView*)numberSelectView currentNumber:(NSInteger)currentNumber;
@ender
@interface NumberSelectView : UIView
//服务器获得的数字
@property (nonatomic, assign) NSInteger defaultNumber;
//选择的最小值
@property (nonatomic, assign) NSInteger minNumber;
//当前显示的数字
@property (nonatomic, assign) NSInteger currentNumber;
@property (nonatomic, assign) id<NumberSelectViewDelegate> delegate;
@end
.m文件
#import "NumberSelectView.h"
@interface NumberSelectView()
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UIButton *cutButton;
@end
@implementation NumberSelectView
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
self.backgroundColor = [UIColor clearColor];
[self setView:self.frame];
}
return self;
}
-(void)setView:(CGRect)frame
{
UIButton* addButton = [UIButton buttonWithType:UIButtonTypeCustom];
addButton.frame = CGRectMake(frame.size.width/2 + 15, 0, frame.size.width/2 - 15, frame.size.height);
addButton.titleLabel.font = [UIFont systemFontOfSize:22];
[addButton setTitle:@"+" forState:UIControlStateNormal];
[addButton setBackgroundImage:[UIImage imageWithColor:[UIColor WhiteColor] cornerRadius:5] forState:UIControlStateNormal];
[addButton addTarget:self action:@selector(addNumber:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:addButton];
_cutButton = [UIButton buttonWithType:UIButtonTypeCustom];
_cutButton.frame = CGRectMake(0, 0, frame.size.width/2 - 15, frame.size.height);
_cutButton.titleLabel.font = [UIFont systemFontOfSize:22];
[_cutButton setTitle:@"-" forState:UIControlStateNormal];
[_cutButton setBackgroundImage:[UIImage imageWithColor:[UIColor grayColor] cornerRadius:5] forState:UIControlStateNormal];
[_cutButton addTarget:self action:@selector(cutNumber:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_cutButton];
_numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2 - 15, 0, 30, frame.size.height)];
_numberLabel.backgroundColor = [UIColor clearColor];
_numberLabel.font = [UIFont systemFontOfSize:14];
_numberLabel.textColor = [UIColor darkGrayColor];
_numberLabel.textAlignment = NSTextAlignmentCenter;
_numberLabel.text = @"0";
[self addSubview:_numberLabel];
}
-(void)setDefaultNumber:(NSInteger)defaultNumber
{
_currentNumber = defaultNumber;
[_cutButton setEnabled:_currentNumber>_minNumber];
_numberLabel.text = [NSString stringWithFormat:@"%d",defaultNumber];
}
-(void)setMinNumber:(NSInteger)minNumber
{
_minNumber = minNumber;
[_cutButton setEnabled:_currentNumber>_minNumber];
}
-(void)setCurrentNumber:(NSInteger)currentNumber
{
_currentNumber = currentNumber;
[_cutButton setEnabled:_currentNumber>_minNumber];
_numberLabel.text = [NSString stringWithFormat:@"%d",_currentNumber];
}
- (void)addNumber:(UIButton*)addButton
{
_currentNumber++;
_numberLabel.text = [NSString stringWithFormat:@"%d",_currentNumber];
if (_delegate != nil && [_delegate respondsToSelector:@selector(numberSelectView:currentNumber:)]) {
[_delegate numberSelectView:self currentNumber:_currentNumber];
}
[_cutButton setEnabled:(_currentNumber > _minNumber)];
}
- (void)cutNumber:(UIButton*)cutButton
{
_currentNumber--;
_numberLabel.text = [NSString stringWithFormat:@"%d",_currentNumber];
if (_delegate != nil && [_delegate respondsToSelector:@selector(numberSelectView:currentNumber:)]) {
[_delegate numberSelectView:self currentNumber:_currentNumber];
}
[cutButton setEnabled:!(_minNumber == _currentNumber)];
}
@end
在用到该View的类里,在delegate方法里写具体事件。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。