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

tato123

macrumors newbie
Original poster
May 21, 2009
4
0
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]
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.