如何在点击iphone中的段控件上更改标签文本

时间:2012-08-28 08:04:43

标签: iphone xcode uisegmentedcontrol

我在iphone应用程序中使用segmentedControl我想要的时候

是,否则

我还有一个标签我希望当选择YES时标签应该显示YES其他明智的

但它没有改变我的代码

  -(IBAction)segmentedControlIndexChanged{

   switch (self.segmentedControl.selectedSegmentIndex) {
    case 0:
        self.segmentLabel.text =@"YES";
        break;
    case 1:
        self.segmentLabel.text =@"NO";
        break;

    default:
        break;

}


   }

2 个答案:

答案 0 :(得分:2)

使用非常简单     的setTitle:forSegmentAtIndex:

所以代码看起来应该是

NSString *str;
switch (self.segmentedControl.selectedSegmentIndex) {
case 0: {
    str = @"YES";
    break;
}
case 1: {
    str = @"NO";
    break;
}
default:
    break;

[self.segmentedControll setTitle:str forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]

或更简单的版本

if (self.segmentedControl.selectedSegmentIndex == 0) {
    [self.segmentedControll setTitle:@"YES" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}
else if (self.segmentedControl.selectedSegmentIndex == 1) {
    [self.segmentedControll setTitle:@"NO" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}

答案 1 :(得分:1)

enter image description here

查看图像并按照正确的布线(选择值更改选项)。

-(IBAction)valueChanged:(id)sender
{

if (self.segmentedControl.selectedSegmentIndex == 0) {
    [self.segmentedControll setTitle:@"YES" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}
else if (self.segmentedControl.selectedSegmentIndex == 1) {
    [self.segmentedControll setTitle:@"NO" forSegmentAtIndex:self.segmentedControl.selectedSegmentIndex]
}

}