質問

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;}
役に立ちましたか?

解決 2

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.

他のヒント

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.

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top