PDA

View Full Version : Adding a percent in a NSTextField




jweinraub
May 11, 2013, 01:50 AM
I got an issue here. If I don't format it with the percent thingy I get the correct data. So I know the algorithm is correct. If I use the number formatter, I get bizarre data. For instance, one set of data should return 6% and I got 600%. I thought it was a digit padding issue, but I did other datasets that should had returned 1% and I got 750% instead.

Here is my code:


// ... snip

double Level = level( Points );
double Progress = progress( Points);
[lblLevel setDoubleValue:(Level)];
[pbLevel setDoubleValue:(Progress)];

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterPercentStyle];
// Any other format settings you want
NSString* formattedNumber = [formatter stringFromNumber: [NSNumber numberWithDouble: Progress]];
[lblPercent setStringValue:(formattedNumber)];

// ... snip


I am used to coding in C#/C++ so I am used to the old hungarian/camel text style of variable names. I was told by others I don't follow the Cocoa paradigm.

So since it works w/o using the number formatter, as if I just used a standard setIntVal on the label, it works fine. I just wished I can concatenate the double/int with char *! I am obviously new to this so I am eager to learn. Thank you for any help!



larswik
May 11, 2013, 02:17 AM
I have never used NSNumberFormatter before but I would have written something like below. But NSNumberFormatter might be a better way to go.


double Level = 23.45;
NSString *string = [NSString stringWithFormat:@"%.2f%@",Level, @"%"];
[lblPercent setStringValue:string];
//Result 23.45%


That is just off the top of my head if I needed to add a % sign in.

Just my 2 cents.

ArtOfWarfare
May 11, 2013, 09:38 AM
You shouldn't capitalize the first letter of the name of anything other than a class name.

Also, you seem to have a large amount of completely unnecessarily ()'s.

Anyways, I think it might be helpful for you to know that in C, a percent symbol in a string should be escaped like this: "%%"

Edit: I didn't read your post very well, clearly. Sorry, maybe someone else will be more helpful.

jweinraub
May 11, 2013, 01:26 PM
I have never used NSNumberFormatter before but I would have written something like below. But NSNumberFormatter might be a better way to go.


double Level = 23.45;
NSString *string = [NSString stringWithFormat:@"%.2f%@",Level, @"%"];
[lblPercent setStringValue:string];
//Result 23.45%


That is just off the top of my head if I needed to add a % sign in.

Just my 2 cents.


This actually worked for what I needed, so thank you for that!

You shouldn't capitalize the first letter of the name of anything other than a class name.

Also, you seem to have a large amount of completely unnecessarily ()'s.

Anyways, I think it might be helpful for you to know that in C, a percent symbol in a string should be escaped like this: "%%"

Edit: I didn't read your post very well, clearly. Sorry, maybe someone else will be more helpful.

The capital letters were variables since I accidentally used the same names for functions. The parentheses are functions since a variable is getting populated from a function, hence the parentheses.

chown33
May 11, 2013, 02:49 PM
The parentheses are functions since a variable is getting populated from a function, hence the parentheses.

These parentheses are all superfluous:
[lblLevel setDoubleValue:(Level)];
[pbLevel setDoubleValue:(Progress)];

...
[lblPercent setStringValue:(formattedNumber)];



Regarding NSNumberFormatterPercentStyle, see Data Formatting Guide : Number Formatters (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfNumberFormatting10_4.html), heading "Percentages".

jweinraub
May 11, 2013, 09:20 PM
Thank you for the link. Does Apple have a full PDF of everything rather than just chapters/units? I am learning MacOS development first before I move onto iOS. I guess though there are much differences like how I used NSTextField for my outlets I think for iOS they use UIText or something? Where can I find documentation on the differences between the two environments?

Thanks :)

:apple:

ArtOfWarfare
May 12, 2013, 08:49 AM
Thank you for the link. Does Apple have a full PDF of everything rather than just chapters/units? I am learning MacOS development first before I move onto iOS. I guess though there are much differences like how I used NSTextField for my outlets I think for iOS they use UIText or something? Where can I find documentation on the differences between the two environments?

Thanks :)

:apple:

The difference is OS X uses Cocoa and App Kit while iOS uses Cocoa Touch and UIKit. Different names for libraries that have basically the same capabilities (although I'm finding WebView seems to be worlds more capable than UIWebView... That's an exception to a general rule.)

If you're not yet familiar with C, you should definitely learn it first. If you're already familiar with C, I would suggest learning iOS first rather than OS X, especially if that's your end goal, because iOS has a lot more beginner friendly materials available and because the frameworks strike me as less redundant (and so less confusing to learn.)

jweinraub
May 12, 2013, 10:08 AM
Yes my end goal is iOS. I thought MacOS would be a better starting point but I'm beginning to learn it should had been the other way around.

My background is C++ so I have it's philosophy on objects and whatnot. I never picked up on the tokens that scanf/printf uses whenever I tried looking at pure C.

ArtOfWarfare
May 12, 2013, 11:49 AM
Yes my end goal is iOS. I thought MacOS would be a better starting point but I'm beginning to learn it should had been the other way around.

My background is C++ so I have it's philosophy on objects and whatnot. I never picked up on the tokens that scanf/printf uses whenever I tried looking at pure C.

Learning pure C probably isn't necessary, then, if you already know C++.

Duncan C's post over at iPhone Dev SDK might be helpful for you:
http://iphonedevsdk.com/forum/iphone-sdk-development/100555-transition-from-c-to-objective-c.html

jweinraub
May 12, 2013, 12:25 PM
Thank you for that! I really appreciate it. Any books you can recommend for MacOS and/or iOS?

jweinraub
May 12, 2013, 07:57 PM
Do you mind if I ask you a second question?
I have a second set of NSTextFields. I have a submit/reset button on the form as well. However, I like the buttons to be disabled if the textfields are empty, as if they fill it in, it enables, if they delete it, it redisables.

I am so used to Visual Studio in C# where I just add events and be done. So I am rather confused with the Xcode paradigm of doing things.

Anyway, this is what I did....
I made the outlets for the textfield, I made an action for editChange

and it seems not to be working. I already made the two fields disabled in the Interface Maker.

But it seems not to work. Any suggestions is also greatly appreciated!

Thanks so much!


- (IBAction)editingChanged
{
NSUInteger lenUser = [[txtUser stringValue] length];
NSUInteger lenPass = [[txtPass stringValue] length];
//[btnReset setState:NSOnState];


[lblDebug setIntegerValue:lenUser]; // get the value for debugging


if ( lenUser > 0 && lenPass > 0 )
{
[btnLogin setEnabled:YES];
[btnReset setEnabled:YES];
}
else
{
[btnLogin setEnabled:NO];
[btnReset setEnabled:NO];
}
}

ArtOfWarfare
May 12, 2013, 09:03 PM
I believe Cocoa Bindings would make short (an codeless) work of that task, although I don't have much experience with it... I think it'd entail binding each text field's value to the Burton's enabled field (more enabled fields will be created for each one you use) and then I think there will be a toggle somewhere to pick whether they're joined together with logical ANDs or logical ORs.

I'm giving a deliberately crappy answer that I didn't research particularly well. Google some of the keywords from my post, if you don't understand what I'm saying.

jweinraub
May 12, 2013, 10:21 PM
No worries, the code I typed in was from Googled code, but it was more of UI stuff for iOS and I converted it to NS so as for my code I am not sure where it is failing I guess it isn't entering the outlet/action. I think I have the outlet going both directions, perhaps that isn't allowed? These drawings is still very awkward for me, since I am used to Windows style of development from work and hate to say it, but adding a method from the method explorer and adding the code to the function is a bit easier. And again, i am also more fluent in C++ than C, but the Objective-C where it uses Smalltalk style messaging will take some getting used to. Thanks though.

jweinraub
May 12, 2013, 11:27 PM
Okay, not sure what I did here, but I had so many open tabs but I got it to work.



- (void)controlTextDidChange:(NSNotification *)notification
{
// NSLog(@"textDidChange");
NSUInteger lenUser = [[txtUser stringValue] length];
NSUInteger lenPass = [[txtPass stringValue] length];

if ( lenUser > 0 && lenPass > 0 )
{
[btnLogin setEnabled:YES];
[btnReset setEnabled:YES];
}
else
{
[btnLogin setEnabled:NO];
[btnReset setEnabled:NO];
}

}


I put the little line thingy to the AppDelegate rather than appcontroller.
Declared the function in the .h file obviously.
And it actually worked when I compiled it.
What I don't get is, the reset button programmatically deletes the contents of the field, but I would think the field, no the value should be zero, by nature, should then automatically lock up the cells, but it didn't because I never typed it (the backspace I assume) because if I do delete it manually it does lock up again. So, naturally I put the button code to lock up in the reset area.

I think my mini tutorial app is good enough, I can probably "graduate" and convert this beast into an iOS app since that was my original goal.

Thanks for all your help, it definitely got me where I needed to be so I definitely learned something!