#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-Create counters
-(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, _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, _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";
}
}
operation on keyboard input
-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{
if ([string isequaltostring:@ "\ n"])
{
return YES;
}
NSString * tobestring = [Textfield.text stringbyreplacingcharactersinrange:range withstring:string]; Get the contents of the input box
if (_myfield = = TextField)//
{
if ([tobestring IntegerValue] > 10) {//Popup warning if input box content is greater than 10
Uialertcontroller * Alertcontroller = [Uialertcontroller alertcontrollerwithtitle:@ "hint" message:@ "the value entered cannot exceed 10" Preferredstyle:uialertcontrollerstylealert];
Uialertaction *cancelaction = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ ( Uialertaction *action) {
}];
Uialertaction *otheraction = [uialertaction actionwithtitle:@ "OK" 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];
}
Display borders need to be imported #import <QuartzCore/QuartzCore.h>
Custom Uistepper with text input box in the middle