Allowing single digit in UITextField in iOS

前端 未结 13 1437
耶瑟儿~
耶瑟儿~ 2020-12-05 03:55

I have a Verification ViewController, I get 4 digit verification code by SMS and I need to enter those code to login, I have created the ViewController

13条回答
  •  庸人自扰
    2020-12-05 04:08

    Try this sample tutorial passcode lock

    ViewController.h

    #import 
    @interface ViewController : UIViewController
    {
     IBOutlet UITextField *txtPassword;
    }
     @end
    

    ViewController.m

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    txtPassword.delegate=self;
    }
    
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
    {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 1) ? NO : YES;
    }
    

提交回复
热议问题