1

答案.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。

4

1 回答 1

2

我仔细检查了一下,确实我没有连接 UISegmentedControl 大声笑。谢谢!

于 2011-03-06T22:21:34.000 回答