Search code examples
objective-ccocoanstextfield

NSTextField - actions within *textField


I need help using NSTextField and its inputs/outputs.

In a functioning code I have three different NSTextFields (in Cocoa + Obj-C) I know how calculate result from more integer inputs...

--- AppController.h ---
@interface AppController : NSObject {
   IBOutlet NSTextField *firsTextField;    // 1st Outlet
   IBOutlet NSTextField *secondTextField;  // 2nd Outlet
   IBOutlet NSTextField *resultTextField;  // 3rd Outlet
}
- (IBAction)result:(id)sender;
@end

--- AppController.m ---
@Implementation AppController
- (IBAction)result:(id)sender {
   double first = [firstTextField doubleValue];   // set value 1st outlet
   double second = [secondTextField doubleValue]; // set value 2nd outlet
   double result = first + second;                // count result
   [resultTextField setDoubleValue:result];       // set value 3rd outlet
}
@end

But while I try do the same thing in only one NSTextField, I don't know how to do it... My idea is, that process should be following:

  1. Set 1st integer (input)
  2. *Choose math function "/, , -, +" (method/action)
  3. Set 2nd integer (input)
  4. Result (method/action) for calculating above mentioned inputs based on math function

But it is actually all what I am able to explain... problem is that I don't know how I can store 1st input value (before choosing math function) and how to count up result between 1st and 2nd input value.

Thank you in advance for every tip / link / reference / tutorial / etc.


Solution

  • after what I studying your code ... I'm finally got functional code ;-)

    at first... here is how looks app: enter image description here

    and here is the code:

    ---------- CalcController.h -----------
    #import <Foundation/Foundation.h>
    #import <Cocoa/Cocoa.h>
    
    @interface CalcController : NSObject {
        IBOutlet NSTextField *textField;
    }
    
    @property (weak) IBOutlet NSTextField *operandTextField;
    @property (weak) IBOutlet NSTextField *resultTextField;
    
    @property (nonatomic) double value1;
    @property (nonatomic) double value2;
    @property (nonatomic) double result;
    @property (nonatomic) NSString *operator;
    
    - (IBAction)plus:(id)sender;
    - (IBAction)result:(id)sender;
    
    @end
    
    
    ---------- CalcController.m -----------
    #import "CalcController.h"
    
    @implementation CalcController
    
    @synthesize operandTextField;
    @synthesize resultTextField;
    
    @synthesize value1;
    @synthesize value2;
    @synthesize result;
    @synthesize operator;
    
    - (IBAction)plus:(id)sender {
        value1 = [textField doubleValue];
        NSLog(@"operand1 = %lf", value1);   // for test and see value in console
        [textField setStringValue:@""];     // for clear text after 1st input done
        operator = @"+";
    }
    
    - (IBAction)result:(id)sender {
    
        value2 = [textField doubleValue];
        NSLog(@"operand2 = %lf", value2);   // for test and see value in console
        
        if ([operator isEqualToString:@"+"]) {
            result = value1 + value2;
            NSLog(@"value1: %lf + value2: %lf = result: %lf", value1, value2, result); // for tests and see values in console
        }
        
        [resultTextField setDoubleValue:result];
        operator = @"";
    }
    
    @end
    

    I Think that working good .... now it is time to add next functions, buttons with numbers etc...

    Thanks for showing the right way to do this!