Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 11, 2013, 02:50 AM   #1
jweinraub
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
Adding a percent in a NSTextField

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:

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!
jweinraub is offline   0 Reply With Quote
Old May 11, 2013, 03:17 AM   #2
larswik
macrumors 65816
 
Join Date: Sep 2006
I have never used NSNumberFormatter before but I would have written something like below. But NSNumberFormatter might be a better way to go.

Code:
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.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old May 11, 2013, 10:38 AM   #3
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
ArtOfWarfare is offline   0 Reply With Quote
Old May 11, 2013, 02:26 PM   #4
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
Quote:
Originally Posted by larswik View Post
I have never used NSNumberFormatter before but I would have written something like below. But NSNumberFormatter might be a better way to go.

Code:
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!

Quote:
Originally Posted by ArtOfWarfare View Post
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.
jweinraub is offline   0 Reply With Quote
Old May 11, 2013, 03:49 PM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by jweinraub View Post
The parentheses are functions since a variable is getting populated from a function, hence the parentheses.
These parentheses are all superfluous:
Code:
   [lblLevel setDoubleValue:(Level)];
   [pbLevel setDoubleValue:(Progress)];

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

Regarding NSNumberFormatterPercentStyle, see Data Formatting Guide : Number Formatters, heading "Percentages".
chown33 is offline   0 Reply With Quote
Old May 11, 2013, 10:20 PM   #6
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
Thanks!

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

jweinraub is offline   0 Reply With Quote
Old May 12, 2013, 09:49 AM   #7
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by jweinraub View Post
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

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.)
ArtOfWarfare is offline   0 Reply With Quote
Old May 12, 2013, 11:08 AM   #8
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
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.
jweinraub is offline   0 Reply With Quote
Old May 12, 2013, 12:49 PM   #9
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by jweinraub View Post
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...jective-c.html
__________________
Don't tell me Macs don't last: 2007 iMac, 2007 Mac Mini, 2008 MacBook Air, all Vintage.
(iMac obsoletion: April 28, 2015, MBA: October 14, 2015, Mac Mini: March 9, 2016)
ArtOfWarfare is offline   0 Reply With Quote
Old May 12, 2013, 01:25 PM   #10
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
Thank you for that! I really appreciate it. Any books you can recommend for MacOS and/or iOS?
jweinraub is offline   0 Reply With Quote
Old May 12, 2013, 08:57 PM   #11
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
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!

Code:
- (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];
   }
}
jweinraub is offline   0 Reply With Quote
Old May 12, 2013, 10:03 PM   #12
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
ArtOfWarfare is offline   0 Reply With Quote
Old May 12, 2013, 11:21 PM   #13
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
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 is offline   0 Reply With Quote
Old May 13, 2013, 12:27 AM   #14
jweinraub
Thread Starter
macrumors regular
 
Join Date: Jun 2007
Location: Sol III
Okay, not sure what I did here, but I had so many open tabs but I got it to work.

Code:
- (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!
jweinraub is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Using NSTextField to save text into cutom format at a temp location SirFurtWiggle Mac Programming 1 Apr 24, 2014 10:16 AM
iPhone: Can not hide percent in statusbar sstyle Jailbreaks and iOS Hacks 10 Jan 25, 2014 04:23 AM
Adding a Custom Phone Label when adding or Editing contacts Chris Kavanagh iOS 7 0 Oct 20, 2013 05:28 PM
NSTextField focus don't work if is added programmatically Ivang Mac Programming 5 Oct 1, 2013 10:27 AM
Have a NSTextField Popup on button click hightekrednek Mac Programming 0 Aug 7, 2012 08:41 PM

Forum Jump

All times are GMT -5. The time now is 06:57 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC