首先,我有一個calculatorBrain:一個簡單的計算器,Objective-C的
@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)
}
}
ViewController.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
後?謝謝!
你有沒有alloc和初始化calculatorBrain即大腦的對象? – Rushi
我不能使用sender.currentTitle,我alwasy使用'[發件人currentTitle]' –
「sender.currentTitle是沒有NULL!」你確定嗎? Apple文檔指出,該屬性可能爲零。 –