简体   繁体   English

我可以通过编程方式重绘UISegmentedControl吗? 怎么样?

[英]Can I redraw a UISegmentedControl programatically? How?

Say I have a UITextField (default blank), a UISegmentedControl (default 2 segments named "1" & "2"), and a UILabel (default text "None") in my nib . 假设我的笔尖中有一个UITextField(默认为空白),一个UISegmentedControl(默认为2个分别名为“ 1”和“ 2”的段)和一个UILabel(默认文本为“ None”)。 I want to be able to do the following: 我希望能够执行以下操作:

  1. I want to enter a number in the UITextField, say "3" 我想在UITextField中输入数字,说“ 3”
  2. I then want to redraw the UISegmentedControl to have 3 segments named "1", "2", and "3." 然后,我想重绘UISegmentedControl使其具有3个分别为“ 1”,“ 2”和“ 3”的段。
  3. I then want to select the segment in the UISegmentedControl, and reflect the selection in the UILabel. 然后,我想在UISegmentedControl中选择细分,并在UILabel中反映选择。 So if I select "3", UILabel will show "3" 因此,如果我选择“ 3”,UILabel将显示“ 3”

I know how to set an IBAction that will catch the number entered in the UITextField, but how do #2 above, that is how do I "redraw" the UISegmentControl to have 3 segments named "1", "2", and "3?" 我知道如何设置一个IBAction来捕获在UITextField中输入的数字,但是上面的#2怎么做,也就是我如何“重绘” UISegmentControl使其具有3个分别为“ 1”,“ 2”和“ 3”的段?

I know how to do #3. 我知道该怎么做#3。

You can do it by using the insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL) method. 您可以通过使用insertSegmentWithTitle:(NSString *)atIndex:(NSUInteger)animation:(BOOL)方法来实现。

Like: 喜欢:

int indexOfSegment = yourSegmentController.numberOfSegments;
[yourSegmentController insertSegmentWithTitle:yourTextfield.text atIndex:indexOfSegment animated:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM