Search code examples
iosreturn-valueglobalization

How to Create Dynamic Value Which Can use Entire App


Basiclly, I have a button which created value according to finish some math process like +, - , /, *. Question is when I get some value, I needed to use that value again when I press button second time. Let me explain Basicly,

When I clicked button process like,

      int examplea  = [txt_example.text intValue];
      int exB = 5000;
      int exC = exB - examplea;

This is the first time I push the button, my lastest value is exC When I input text field another value and clicked it same process will start but one difference:

    int examplea = [txt_example.text intValue];
    int exB = exC;
    int exC ( this new value which will be calculated )  = exB - examplea;

How can I create process like this?


Solution

  • Something like this

    in the .h file

    @interface MyCalculator : NSObject
    - (int) doCalculation;
    @property (assign, nonatomic) int exB;
    @end
    

    in the .m file

        @implementation MyCalculator
    
    - (id) init
    {
        self = [super init];
        if (self)
        {
            _exB = 5000;
        }
        return self;
    }
    
        - (int) doCalculation
        {
            int examplea  = [txt_example.text intValue];
            int exC = self.exB - examplea;
            self.exB = exC;
            return exC;
        }
        @end
    
    
    
    ....
    MyCalculator* myCalculator = [[MyCalculator alloc] init];
    
    ...
    
    [myCalculator doCalculation];
    

    This isn't the full solution as there are several questions about what your code will do, but it will illustrate the principle of how to do it. The last line is what gets called when the button is pressed.

    I don't know where txt_example.text is coming from, but it might be better if that is passed to doCalculation as a parameter.