Search code examples
iosnsstringuitextfieldparameter-passingmessage-passing

passing values string values to an object and recieving a string in return in ios


Hi a very simple app it takes in 2 arguments via 2 text boxes, and then totals them and displays them in a label called result. The idea is to have it handled via an object called brain, for which in the later part i have given the code. problem is foo is zero and when you click the button the result goes to nothing. The plan is to use this to build a better model view architecture for a bigger app i have completed.

 #import "calbrain.h"
    #import "ImmyViewController.h"
    @interface ImmyViewController ()
    @property (nonatomic, strong) calbrain *brain;
    @end
    @implementation ImmyViewController
    @synthesize brain;
    @synthesize num1;
    @synthesize num2;
    @synthesize result;

        -(calbrain *) setBrain
        {
            if (!brain) {
                    brain = [[calbrain alloc] init];

            }

            return brain;
         }

    - (IBAction)kickit:(UIButton *)sender {
                 NSString *number1 = self.num1.text;
                 NSString *number2 = self.num2.text;

            NSString *foo;

            foo = [brain calculating:number1 anddouble:number2];
            self.result.text = foo;
            //  self.result.text = [brain calculating:self.num1.text    anddouble:self.num2.text];

    }
        @end

        @implementation calbrain

    -(NSString *) calculating:(NSString *)number1 anddouble:(NSString *)number2

    {
            double numb1 = [number1 doubleValue];
            double numb2 = [number2 doubleValue];

            double newresult = (numb1 + numb2);


            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

            NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber n     numberWithFloat:newresult]];

            return numberAsString;}

Solution

  • i did just that, i came to the conclusion the setter for brain isnt working properly

    i put the alloc init line of code before i needed to alloc init the brain, and it works fine, i stubbed out the setter,

    i will go back and see why it wasnt overriding the setter made by properties, but interesting stuff none the less. it means i can change my actual larger app to have a cleaner more organised architecture.

    thanks for your time.