0

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;}
Imran Raja
  • 123
  • 1
  • 3
  • 11

3 Answers3

0

Check your brain using NSLog in the (IBAction)kickit:(UIButton *)sender function. I guess you didn't initialise brain. If this is not the case, you need to provide more code.

brianLikeApple
  • 4,262
  • 1
  • 27
  • 46
  • @synthesize brain = _brain; -(calbrain *) brain{ if (!_brain) { _brain = [[calbrain alloc]init]; } return _brain; } – Imran Raja Jun 20 '13 at 16:18
0

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.

Imran Raja
  • 123
  • 1
  • 3
  • 11
  • he problem lay in my setter method this is the correct code – Imran Raja yesterday @synthesize brain = _brain; -(calbrain *) brain{ if (!_brain) { _brain = [[calbrain alloc]init]; } return _brain; } – Imran Raja yesterday – Imran Raja Jun 22 '13 at 02:00
0

Try initializing your brain object in viewDidLoad() using your setter method. You have to call setter method to get your brain object initialized.

Something like this

viewDidLoad()
{
  brain = [self setBrain]; 
  //You can also do this
  brain = [[calbrain alloc] init];
} 

and use that brain object in your (IBAction)kickit: method.

Hope this helps.

Priyatham51
  • 1,864
  • 1
  • 16
  • 24