Clear textfield notification?

Discussion in 'iOS Programming' started by Darkroom, Aug 25, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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";
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    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;
    	}
    
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    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 = @"";
    	}
    
     

Share This Page