-1

起初,我有一个calculatorBrain:

@interface calculatorBrain : NSObject {
    NSString *theOperator;
}

@property (assign, nonatomic) NSString *theOperator;

-(float)performOperation:(float)a andOperandB:(float)b;
@end

然后, 中的一个函数ViewController,链接到一个button.

ViewController.m 的一部分

@implementation ViewController
@synthesize result, brain;

-(IBAction)operatorPressed:(UIButton *)sender
{
    if([sender.currentTitle isEqualToString:@"AC"]) {
        result.text = @"0";
    } else if ([sender.currentTitle isEqualToString:@"."]) {
        if (0 == [result.text rangeOfString:@"."].length)
        result.text = [result.text stringByAppendingString:@"."];
    } else {
        brain.theOperator = sender.currentTitle;
        NSLog(@"%@", brain.theOperator); // return (null)
    }
}

视图控制器.h

@interface ViewController : UIViewController {
    IBOutlet UILabel *result;
    calculatorBrain *brain;
}

@property (assign, nonatomic) IBOutlet UILabel *result;
@property (strong, nonatomic) calculatorBrain *brain;

-(IBAction)digitPressed:(UIButton *)sender;
-(IBAction)operatorPressed:(UIButton *)sender;

@end

问题是,为什么我按下后什么都不设置button?谢谢!

4

2 回答 2

0

从您的代码片段来看,您似乎希望合成此属性。您可以在网络上的各种教程中阅读更多相关信息。

确切的行为将取决于所使用的 Obj-C 版本。我建议您阅读上述教程,以便更好地理解您的代码。

于 2013-01-02T09:11:19.060 回答
0

试试这个:

 -(IBAction)operatorPressed:(UIButton *)sender
    {
        brain = [[calculatorBrain alloc] init] autorelease];
        if([sender.currentTitle isEqualToString:@"AC"]) 
        {
            result.text = @"0";
        } 
        else if ([sender.currentTitle isEqualToString:@"."]) 
        {
            if (0 == [result.text rangeOfString:@"."].length)
                result.text = [result.text stringByAppendingString:@"."];
        } 
        else 
        {
            brain.theOperator = sender.currentTitle;
            NSLog(@"%@", brain.theOperator); // return (null)
        }
    }
于 2013-01-02T13:24:24.913 回答