【问题标题】:Change button text inside segment control更改段控件内的按钮文本
【发布时间】:2013-12-07 10:18:53
【问题描述】:

我在 .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 说“使用未声明的标识符”按钮“,但在 .h 文件中 -(IBAction)button:(id)sender;

【问题讨论】:

  • 这不是 UIButton 的声明。你已经声明了一个方法。 D

标签: ios undeclared-identifier uisegmentedcontrol


【解决方案1】:

如果我没听错,您想动态更改段的标题。你可以用 UISegmentedControl 的方法来做到这一点

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

您需要在您的 xib 文件中拥有段控件的 outlet 属性。然后你就这样做:

[self.mySegmentControl setTitle:@"New title" forSegmentAtIndex:0];

【讨论】:

  • 不,我想更改段控件内的操作按钮。例如:如果 selectedSegmentIndex == 0,则内部按钮命名为 Button,当 selectedSegmentIndex == 1 时,内部按钮命名为 Button2。我有问题,因为它不是默认按钮,而是操作按钮
  • 我不确定您所说的“分段控件内的操作按钮”是什么意思。如果这是您想要在使用段控件时更改的某个 UIButton...您需要该按钮的 IBOutlet 属性。然后你就可以用[button setTitle:@"Button!"];设置它的标题了
【解决方案2】:

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

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

 @interface yourViewController : UIViewController {

   UIButton *button;

 }

对于更改段控制的标题,您可以这样做:

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

如果您想更改 UIButton 的标题:

[button setTitle:@"Title" forState: UIControlStateNormal];

【讨论】:

    【解决方案3】:

    - (IBAction)button:(id)sender; 是方法而非标识符

    所以要更改标题文本的操作按钮,您必须编写以下代码

    -(IBAction)sectionswitch:(id)sender {
    
        if (control.selectedSegmentIndex == 0) {
            ...
            [sender setTitle:@"AAAAA" forSegmentAtIndex:0];
            ...
        }
    
        if (control.selectedSegmentIndex == 1) {
            ...
            [sender setTitle:@"BBBBB" forSegmentAtIndex:1];
            ...
        }
    
    }
    

    【讨论】:

    • 我不想更改段控制的标题。每个段控件内都有一个操作按钮(电话号码)。所以如果 control.selectedSegmentIndex == 0 我想改变数字,如果 control.selectedSegmentIndex == 1 我想改变它到另一个数字
    • 对于更改操作按钮,您必须首先为该按钮提供标记号并应用以下代码 [(UIButton *)[sender viewWithTag:TagNo] setTitle:@"PhoneNo" forState:UIControlStateNormal];跨度>
    【解决方案4】:

    斯威夫特 4

        segmentButton.setTitle("hello", forSegmentAt: 0)
    

    【讨论】:

      猜你喜欢
      • 2011-06-11
      • 1970-01-01
      • 2018-07-19
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      相关资源
      最近更新 更多