I'm not sure why I got down voted for a legitimate question, but I solved my problem so others can avoid asking it.
Your class needs to conform to the UITextFieldDelegate in order for this to work.
I created an action based on the UIControlEventEditingChanged event in the viewDidLoad function.
[dateUIText addTarget:self
action:@selector(dateTextFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
This is what the dateTextFieldDidChange function looks like when adding the "/" after the month:
- (void) dateTextFieldDidChange: (UITextField *)theTextField {
NSString *string = theTextField.text;
if (string.length == 2) {
theTextField.text = [string stringByAppendingString:@"/"];
} else if (string.length > 5) {
theTextField.text = [string substringToIndex:5];
}
}
When the user tries to backspace to make any adjustments to the month the function above won't allow it so this function is then needed to assist:
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@""] && textField.text.length == 3) {
NSString *dateString = textField.text;
textField.text =
[dateString stringByReplacingOccurrencesOfString:@"/" withString:@""];
}
return YES;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…