答案.h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "operator.h"
@interface answer : NSObject/* Specify a superclass (eg: NSObject or NSView) */ {
IBOutlet operator *operator1;
IBOutlet operator *operator2;
IBOutlet UILabel *answerLabel;
IBOutlet UISegmentedControl *operation;
}
- (IBAction)compute:(id)sender;
@end
答案.m:
#import "answer.h"
@implementation answer
- (IBAction)compute:(id)sender {
int selected = operation.selectedSegmentIndex;
switch (selected) {
case 0:
answerLabel.text = [NSString stringWithFormat:@"%d", (int)[operator1 number] + (int)[operator2 number]];
break;
case 1:
answerLabel.text = [NSString stringWithFormat:@"%d", (int)[operator1 number] - (int)[operator2 number]];
break;
case 2:
answerLabel.text = [NSString stringWithFormat:@"%d", (int)[operator1 number] * (int)[operator2 number]];
break;
case 3:
answerLabel.text = [NSString stringWithFormat:@"%d", (int)[operator1 number] / (int)[operator2 number]];
break;
default:
break;
}
}
@end
我遇到的问题是selectedSegmentIndex
总是等于0。