根据索引更改 UIButtons 的标题,就像 UISegmentedControl

Change title of UIButtons based on index, just like UISegmentedControl

当我使用 segmentedControl 时,我会执行以下操作以根据未知 indices(运行前)设置标题:

[self.segment setTitle:@"this Title" forSegmentAtIndex:next];
[self.segment setTitle:@"another Title" forSegmentAtIndex:after];

我正在尝试使用 UIButtons 复制 UISegmentedControl。如何使用 UIButtons?

实现上述代码

我想将 buttons 添加到 array,但如果这样做,我将无法通过 array index 设置标题。有什么建议

当然,您可以将按钮放在数组中,并且必须像分段控件一样使用数组索引。

NSArray *buttons = ... // your array of buttons
[buttons[next] setTitle:@"this Title" forState:UIControlStateNormal];
[buttons[after] setTitle:@"another Title"] forState:UIControlStateNormal];

[按钮设置标题:@"After" forState:UIControlStateNormal];

[按钮设置标题:@"Next" forState:UIControlStateNormal];