简体   繁体   中英

how to display pickerview when user enter first 4 digits values in textfield

I have one UiTextField Called MobileNumber. and two pickerView called Operator and circle. When I enter first 4 digits of my number in textfield it displays the Operator pickerview , how to call it when I enter first 4 digits value in textfield and display the pickerview

You can declare a notification that will be called on every single change in text field.

// Add a "textFieldDidChange" notification method to the text field control.
[textField addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];

Now here on this method textFieldDidChange: you can access your text field and check if user has entered 4 digits you can show the picker.

Use shouldChangeCharactersInRange textField's delegate method for entering numeric character

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
    {
        //This field accepts only numeric entries.
        return NO;
    }
    else //numeric value entered
    {
        NSString *strTxtField = [textField.text stringByAppendingString:string];
        if(strTxtField.length == 4)
        {
            //show pickerview here
            //Set return NO if you don't want more character to be added to textfield
            //return NO;
        }
        return YES;
    }
}

try this code...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(newString.length == 4)
    {
        NSLog(@"show date picker");
       //Set return NO if you don't want more character to be added to textfield
        //return NO;
    }

    return newString.length <= 4;
}

make sure that your text field delegate set properly..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM