是否可以使用swift以编程方式填充UISegmentedControl
的值?以编程方式填充使用swift的UISegmentedControl
1
A
回答
4
let segmentedControl = UISegmentedControl()
segmentedControl.insertSegment(withTitle: "Title", at: 0, animated: true)
segmentedControl.setTitle("Another Title", forSegmentAt: 0)
1
我解决我的问题,使用@ RyuX51 的解决方案,现在我的代码是:
class MyCustomViewController: UIViewController{
@IBOutlet weak var ServicesSC: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
ServicesSC.removeAllSegments()
ServicesSC.insertSegment(withTitle: "Title", at: 0, animated: true)
ServicesSC.setTitle("Another Title", forSegmentAt: 0)
}
}
2
如果我没有弄错,你是说你要添加段“UISegmentedControl”组件编程,而不使用Interface Builder。
是的,这是可能的:
// Assuming that it is an "IBOutlet", you can do this in your "ViewController":
class ViewController: UIViewController {
@IBOutlet weak var segmentedControl: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// remove all current segments to make sure it is empty:
segmentedControl.removeAllSegments()
// adding your segments, using the "for" loop is just for demonstration:
for index in 0...3 {
segmentedControl.insertSegmentWithTitle("Segment \(index + 1)", atIndex: index, animated: false)
}
// you can also remove a segment like this:
// this removes the second segment "Segment 2"
segmentedControl.removeSegmentAtIndex(1, animated: false)
}
// and this is how you can access the changing of its value (make sure that event is "Value Changed")
@IBAction func segmentControlValueChanged(sender: UISegmentedControl) {
print("index of selected segment is: \(sender.selectedSegmentIndex)")
}
}
相关问题
- 1. swift - 以编程方式填充STPPaymentCardTextField
- 2. 以编程方式使用UISegmentedControl文本
- 3. 在Swift中以编程方式更改UIButton的文本(填充)
- 4. 以编程方式填充的DataGridView
- 5. 如何以编程方式填充DataGridView
- 6. 以编程方式填充水平UIScrollView
- 7. 以编程方式填充DataGridView ComboBoxCell
- 8. 以编程方式填充的UITableView无法填充屏幕
- 9. 如何以编程方式在swift 3.0中添加UISegmentedControl?
- 10. 以编程方式填充WPF自动填充框
- 11. 以编程方式使用选中的选项填充CheckBoxList
- 12. 如何以编程方式显示UISegmentedControl?
- 13. 以编程方式更改UISegmentedControl值
- 14. UISegmentedControl以编程方式重置
- 15. 如何以编程方式使用ImageButton填充网格?
- 16. 以编程方式创建并使用JXA填充NSWindow
- 17. 以编程方式使用VB6填充Infopath 2003窗体
- 18. 以编程方式使用android:layout_weight填充TableLayout
- 19. 以编程方式更改ASP.NET GridView中列的样式(填充)
- 20. 如何以编程方式使UICollectionView填充UITableViewCell?
- 21. 如何使只读(以编程方式填充)首选项?
- 22. 使单个按钮宽度以编程方式填充父项
- 23. 用复选框以编程方式填充首选项
- 24. 如何以编程方式用CoreData行填充NSArrayController?
- 25. 以编程方式在WPF中用文本框填充网格
- 26. 以编程方式填充图像以进行动画
- 27. 以编程方式确定分组的UITableView的填充
- 28. 以编程方式填充所有歌曲的列表?
- 29. javascript - 以编程方式计算元素之间的填充
- 30. 以编程方式填充共享点中的选择字段