How can I get the value inside of an NSTextField?
My NSTextField contains doubles like "1.67" or "0.05" etc.
When I try the following:
console.log([priceChange doubleValue]); //priceChange is an NSTextField
//Apply styles
if([priceChange doubleValue] > 0)
[priceChange setTextColor:[NSColor greenColor]];
else if([priceChange doubleValue] < 0)
[priceChange setTextColor:[NSColor redColor]];
else if([priceChange doubleValue] == 0)
[priceChange setTextColor:[NSColor blackColor]];
priceChange is equal to 0 always, even when the value is something else. How can I properly get the value of pr开发者_运维技巧iceChange?
This is precisely what NSNumberFormatter
is for. Give your field a suitable formatter (easy to add/setup in IB), and -objectValue will start returning an NSNumber
.
Try [[priceChange stringValue] floatValue]
. Also make sure there is nothing other than whitespace before the number in the string, or floatValue will return 0.
精彩评论