Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
I've set a notification on my textfield for when it changes. What is the proper way to clear the field? The following is incorrect.

Code:
If ([myTextField.text isEqualToString:@" "])
     [myTextField textFieldShouldClear:YES]

this code insures that the first and/or only char of the entered text can not be whitespace. Renders the spacebar non-visually disabled until actual text is entered.
 
You can use the textField:shouldChangeCharactersInRange:replacementString: to enforce any policies regarding what characters can be entered into the textfield. Return NO if you don't want the text field to be changed.

textFieldShouldClear: is a delegate method. You implement it in your delegate and return YES or NO.

To simply set the text of the textfield to a space or an empty string just set the text property of the object.

myobject.text = @"whatever";
 
ok... so i'm actually trying to use both methods. but testField:shouldChangeCharactersInRange:replacementsString only returns a bool, while i would also like an action to occur. for example, in the code below the text field allows for 20 characters, and if 20 characters is reached, the screen flashes.

my text field has a placeholder string. so if the space is the first character to be entered, i would like the placeholder to continue to show while disallowing the first character to be whitespace

Code:
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
	{
	//Set Maximum Characters Length For textField 
	if (theTextField.text.length >= kTextFieldMaximumCharacterLength && range.length == 0)
		return NO;
		else
		return YES;
	}

- (void)textFieldDidChange:(NSNotification *)notification
	{
	//Disallow First Character As Space
	if ([textFieldSaveMeasurements.text isEqualToString:@" "])
		{
[COLOR="Green"]		//[textFieldSaveMeasurements textFieldShouldClear:YES];
		//return;[/COLOR]
		NSLog(@"Disallow First Character As Space");
		}

	//Enable Or Disable Save Button
	if (textFieldSaveMeasurements.text.length == 0)
		saveButton.enabled = NO;
		else
		saveButton.enabled = YES;
	
	//Text Field Reached Maximum Character Allowance
	if (textFieldSaveMeasurements.text.length == kTextFieldMaximumCharacterLength)
		if ([kDefaults boolForKey:SMConstFlashKey] == YES )
			[self flashFadeIn];
	}

maybe i'm currently too tired to be thinking this thru correctly, but the following simply renders the text field unusable if space has been entered as the first character

Code:
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
	{
	//Set Maximum Characters Length For textField 
	if ((theTextField.text.length >= kTextFieldMaximumCharacterLength && range.length == 0) || ([theTextField.text isEqualToString:@" "]))
		return NO;
		else
		return YES;
	}
 
ok, now that i'm awake :rolleyes:

the following works well

Code:
- (void)textFieldDidChange:(NSNotification *)notification
	{
	//Disallow First Character As Space
	if ([textFieldSaveMeasurements.text isEqualToString:@" "])
		textFieldSaveMeasurements.text = @"";
	}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.