1

我在 .m 文件中有这样的段控制和代码

-(IBAction)sectionswitch:(id)sender {

    if (control.selectedSegmentIndex == 0) {
        UIImage *dekabristov = [UIImage imageNamed:@"dekabristov.png"];
        [image setImage:dekabristov];
    }

    if (control.selectedSegmentIndex == 1) {
        UIImage *fabrika = [UIImage imageNamed:@"fabrika.jpg"];
        [image setImage:fabrika];
    }

}

如何更改段控件内的操作按钮的标题?如果我写[button setTitle:@"Button!"];Xcode 说“使用未声明的标识符”按钮”,但-(IBAction)button:(id)sender;在 .h 文件中

4

4 回答 4

1

如果我说得对,您想动态更改段的标题。你可以用 UISegmentedControl 的方法做到这一点

- (void)setTitle:(NSString *)title forSegmentAtIndex:(NSUInteger)segment

您将需要在您的 xib 文件中具有段控件的出口属性。然后你就这样做:

[self.mySegmentControl setTitle:@"New title" forSegmentAtIndex:0];
于 2013-12-07T10:24:48.297 回答
0

这样,您没有声明一个按钮,而只是一个名为“按钮”的动作。

在您的 .h 文件中,您应该这样做:

 @interface yourViewController : UIViewController {

   UIButton *button;

 }

对于更改段控制的标题,您可以执行以下操作:

 [yourSegmentControl setTitle:@"Button!" forSegmentAtIndex:0]; //0 is the first

如果要更改 UIButton 的标题:

[button setTitle:@"Title" forState: UIControlStateNormal];
于 2013-12-07T10:27:06.367 回答
0

- (IBAction)按钮:(id)发件人;是方法而不是标识符

所以要更改标题 Text of Action 按钮,您必须编写以下代码

-(IBAction)sectionswitch:(id)sender {

    if (control.selectedSegmentIndex == 0) {
        ...
        [sender setTitle:@"AAAAA" forSegmentAtIndex:0];
        ...
    }

    if (control.selectedSegmentIndex == 1) {
        ...
        [sender setTitle:@"BBBBB" forSegmentAtIndex:1];
        ...
    }

}
于 2013-12-07T10:34:54.797 回答
0

斯威夫特 4

    segmentButton.setTitle("hello", forSegmentAt: 0)
于 2018-10-07T08:40:20.807 回答