如何添加从多UISegmentedControl创建的多UILabel

时间:2014-02-02 01:19:36

标签: ios objective-c uilabel

在我的.h文件中:

#import <UIKit/UIKit.h>
@property (weak, nonatomic) IBOutlet UILabel *lblSwResults;

- (IBAction)SegSwOne:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *SegSwOne;
- (IBAction)SegSwTwo:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *SegSwTwo;

@property (weak, nonatomic) IBOutlet UILabel *lblResOne
@property (weak, nonatomic) IBOutlet UILabel *lblResTwo;
<。>文件中的

- (IBAction)SegSwOne:(id)sender {
    switch (self.SegSwOne.selectedSegmentIndex) {
        case 0:
            self.lblResOne.text =@"50";
            break;
        case 1:
            self.lblResOne.text =@"200";
            break;
        case 2:
            self.lblResOne.text =@"400";
            break;
        default:
            break;
    }
}

- (IBAction)SegSwTwo:(id)sender {
    switch (self.SegSwTwo.selectedSegmentIndex) {
        case 0:
            self.lblResTwo.text =@"50";
            break;
        case 1:
            self.lblResTwo.text =@"150";
            break;
        case 2:
            self.lblResTwo.text =@"400";
            break;
        default:
            break;
    }
}

我要做的事情是,如果你选择不同的UISegmentedControl,它会更改UILabel lblSwResults中的数字....所以如果结果标签中的case0和case2 =“450”...

看它是自动的 - 当您在SegSwOne或SegSwTwo中更改值时显示值的动态更改...编码此内容的最佳原因是什么?我是否需要为每个创建一个浮点值,然后为结果???

创建一个NSString公式

任何帮助都很棒或链接,所以我可以找到更多...。?干杯

1 个答案:

答案 0 :(得分:0)

我的情况如下:

创建两个为每个段返回值的方法:

- (int)segmentOneValue:(NSInteger)index {
    static int values[] = { 50, 200, 400 };

    return values[index];
}

- (int)segmentTwoValue:(NSInteger)index {
    static int values[] = { 50, 150, 400 };

    return values[index];
}

然后你的两个分段控制动作将是:

- (IBAction)SegSwOne:(UISegmentControl *)control {
    int valueOne = [self segmentOneValue:control.selectedSegmentIndex];
    int valueTwo = [self segmentTwoValue:self.SegSwTwo.selectedSegmentIndex];

    self.lblResOne.text = [NSString stringWithFormat:@"%d", valueOne];
    self.lblSwResults.text = [NSString stringWithFormat:@"%d", valueOne + valueTwo];
}

- (IBAction)SegSwTwo:(UISegmentControl *)control {
    int valueTwo = [self segmentOneValue:control.selectedSegmentIndex];
    int valueOne = [self segmentTwoValue:self.SegSwOne.selectedSegmentIndex];

    self.lblResTwo.text = [NSString stringWithFormat:@"%d", valueTwo];
    self.lblSwResults.text = [NSString stringWithFormat:@"%d", valueOne + valueTwo];
}