PDA

View Full Version : Help with a Objective-C Cocoa App.




MDMstudios
Mar 18, 2008, 02:06 AM
Hello, I am new to cocoa programing, and am trying to develop a simple program that has one button and two NSTextFields, the object of the program is, that once you press the button one of the NSTextFields subtract its value from the other NSTextFields.
I have the compiler saying there aren't any errors, but when I click on the button nothing happens. I have everything in Interface Builder set up right, so I imagine it must be a code problem
Here is the code for it
MyController.h #import <Cocoa/Cocoa.h>
@interface MyController : NSObject {
IBOutlet NSTextField *amount;
IBOutlet NSTextField *amountToSubtract;
float myAmount;
float myAmountToSubtract;

}
- (IBAction)subtract:(id)sender;
@end

MyController.m @implementation MyController
- (IBAction)subtract:(id)sender {
myAmount = [amount floatValue] - [amountToSubtract floatValue];
[amount stringWithFormat:@"%f", myAmount ];

}
@end



robbieduncan
Mar 18, 2008, 03:26 AM
I assume the line containing stringWithFormat is you trying to set the value displayed in the NSTextField? If so that ain't gonna work. You need to use one of the setXXXX methods, something like setFloatValue: (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/instm/NSControl/setFloatValue:).

Note that it's not really the Cocoa way to format the string and then set it: use a formatter on the NSTextField instead.

MDMstudios
Mar 18, 2008, 03:14 PM
I assume the line containing stringWithFormat is you trying to set the value displayed in the NSTextField? If so that ain't gonna work. You need to use one of the setXXXX methods, something like setFloatValue: (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html#//apple_ref/occ/instm/NSControl/setFloatValue:).

Note that it's not really the Cocoa way to format the string and then set it: use a formatter on the NSTextField instead.

Okay thanks!