Custom Uistepper with text input box in the middle

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.