Help with a Objective-C Cocoa App.

Discussion in 'Mac Programming' started by MDMstudios, Mar 18, 2008.

  1. MDMstudios macrumors member

    Mar 18, 2008
    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
    #import <Cocoa/Cocoa.h>
    @interface MyController : NSObject {
        IBOutlet NSTextField *amount;
        IBOutlet NSTextField *amountToSubtract;
    	float myAmount;
    	float myAmountToSubtract;
    - (IBAction)subtract:(id)sender;
    @implementation MyController
    - (IBAction)subtract:(id)sender {
    	myAmount = [amount floatValue] - [amountToSubtract floatValue]; 
    	[amount stringWithFormat:@"%f", myAmount ];
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    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:.

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

    Mar 18, 2008
    Okay thanks!

Share This Page