#import "MyReserveViewController.h"
@interface MyReserveViewController ()<UITextFieldDelegate>{
UITextField * _myfield;
UIButton * addButn;
UIButton * deleteButn;
}
@end
@implementation MyReserveViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatStepView];
[self creatButton];
// Do any additional setup after loading the view.
}
#pragma mark - 创建计数器
-(void)creatStepView{
_myfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
[email protected]"1";
//_myfield.backgroundColor=[UIColor blueColor];
_myfield.delegate=self;
_myfield.textAlignment=NSTextAlignmentCenter;
_myfield.layer.borderColor=[[UIColor redColor]CGColor];
_myfield.layer.borderWidth=1.0f;
_myfield.keyboardType=UIKeyboardTypeNumberPad;
[self.view addSubview:_myfield];
}
-(void)creatButton{
addButn = [UIButton buttonWithType:UIButtonTypeCustom];
addButn.frame = CGRectMake(0, 0, 30, _myfield.frame.size.height);
[addButn setImage:[UIImage imageNamed:@"111.png"] forState:UIControlStateNormal];
[addButn addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
deleteButn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButn.frame = CGRectMake(0, 0, 30, _myfield.frame.size.height);
[deleteButn setImage:[UIImage imageNamed:@"222.png"] forState:UIControlStateNormal];
[deleteButn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
_myfield.leftView=deleteButn;
_myfield.rightView=addButn;
_myfield.leftViewMode=UITextFieldViewModeAlways;
_myfield.rightViewMode=UITextFieldViewModeAlways;
}
-(void)addAction:(UIButton *)sender{
NSString * changeStr = _myfield.text;
NSInteger minimumNum = 1;
NSInteger maxNum = 10;
if ([changeStr integerValue] > 0 || [changeStr integerValue] < maxNum) {
_myfield.text = [NSString stringWithFormat:@"%ld",[changeStr integerValue]+1];
if ([_myfield.text integerValue] == maxNum) {
addButn.enabled = NO;
}
if ([_myfield.text integerValue] > minimumNum) {
deleteButn.enabled = YES;
}
}
}
-(void)deleteAction:(UIButton *)sender{
NSString * changeStr = _myfield.text;
NSInteger minimumNum = 1;
NSInteger maxNum = 10;
if ([changeStr integerValue] > 0 || [changeStr integerValue] < maxNum) {
_myfield.text = [NSString stringWithFormat:@"%ld",[changeStr integerValue]-1];
if ([_myfield.text integerValue] == minimumNum) {
deleteButn.enabled = NO;
}
if ([_myfield.text integerValue] < maxNum) {
addButn.enabled = YES;
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%@",textField.text);
if ([textField.text integerValue] >= 10) {
addButn.enabled = NO;
}else{
addButn.enabled = YES;
}
if ([textField.text integerValue]<= 1) {
deleteButn.enabled = NO;
}else{
deleteButn.enabled = YES;
}if ([textField.text integerValue] > 10) {
_myfield.text = @"10";
}
}
//对键盘输入的操作
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:@"\n"])
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
if (_myfield == textField) //
{
if ([toBeString integerValue] > 10) { //如果输入框内容大于10则弹出警告
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的数值不能超过10" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
return NO;
}
}
return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
// [self.TF resignFirstResponder];
}
//显示边框需要导入#import <QuartzCore/QuartzCore.h>