Help with a Objective-C Cocoa App.

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

  1. macrumors member

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

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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. thread starter macrumors member

    Joined:
    Mar 18, 2008
    #3
    Okay thanks!
     

Share This Page