textdidchange / edit changed

Discussion in 'iOS Programming' started by tato123, Jun 18, 2009.

  1. tato123 macrumors newbie

    Joined:
    May 21, 2009
    #1
    Issue:

    When updating the .text property of a UITextField object inside of a text change listener an infinite loop occurs. It appears that I'm illegally modifying the object even though edit mode is turned on. I log the value of .text, after the edit occurs, and notice it prints numbers in base 2 (2 4 8 16 32) until it seems to occupy the max int value and then just prints 0s but loops infinitely.


    Synopsis:

    I am attempting to format user text in a user text field as it is inputted. Users are only allowed to type in whole numbers, I use the number keypad. I would like to format the text such that after two digits it automatically inserts a decimal, similar to how atm's format amounts entered and automatically insert a decimal.

    Attempted to:

    I have attempted to do the following (both with the same reaction):

    1. Register a selector to the UITextFieldTextDidChangeNotification, process my input, and update the value.

    2. Use interface builder, tie an ibaction to the editing changed event for the uitext field

    Both end with the same result


    Code Snippet (sorry for the messy code, last attempt was creating new object in memory and attempting to reference that, sorry I didn't release my temp objects):

    Code:
    - (IBAction) atmFormatListener:(id)sender{
    		UITextField *field = (UITextField*)sender;
    		NSNumberFormatter *decimalFormatter = [[NSNumberFormatter alloc] init];
    		[decimalFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    		[decimalFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    	
    		NSNumber *number = [[NSNumber alloc] initWithInt:[field.text intValue]];
    		field.text = [decimalFormatter stringFromNumber:number];
    }
    Thanks for any assistance :)
    [edited, sorry placed old code in block]
     
  2. jerrywickey macrumors member

    Joined:
    Apr 16, 2009
    Location:
    Key West
    #2
    I infer decimalFormatter is a class you wrote. Everything I say here is wrong, if I enfered incorrectly.

    if so
    Code:
    NSNumber *number = [[NSNumber alloc] initWithInt:[field.text intValue]];
    		field.text = [decimalFormatter stringFromNumber:number];
    is where all the action is taking place.

    If you are getting a series of doubles, it has to be coming from a method or function inside decimalFormatter. but it may not be stringFromNumber at all. It is likely some function or instance method that stringFromNumber is calling.

    My guess is, at some point you are adding an integer portion and decimal portion of a number together to arrive at a final return value. Check that code carefully.

    Jerry
     

Share This Page