在View上嵌一個控件,從服務器得到defaultNumber,在頁面加載後默認顯示,而後按"+",個數增長;按"-",個數減小。在個數小於minNumber時,"-"按鈕不可用。
新建一個類numberSelectView,繼承UIView。控件都是在xib文件中使用,須要實現-(id)initWithCoder方法進行初始化。調用類和numberSelectView類的點擊事件的關聯,用協議和代理實現。
代碼最直接了
.h文件segmentfault
#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方法裏寫具體事件。ide