是否可以使用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. 以編程方式使用UISegmentedControl文本
- 2. 在Swift中以編程方式更改UIButton的文本(填充)
- 3. 以編程方式填充的DataGridView
- 4. 以編程方式填充DataGridView ComboBoxCell
- 5. 以編程方式填充WPF自動填充框
- 6. UISegmentedControl以編程方式重置
- 7. 以編程方式使用android:layout_weight填充TableLayout
- 8. 以編程方式使用VB6填充Infopath 2003窗體
- 9. 如何以編程方式使用ImageButton填充網格?
- 10. 以編程方式創建並使用JXA填充NSWindow
- 11. 以編程方式更改ASP.NET GridView中列的樣式(填充)
- 12. 如何使只讀(以編程方式填充)首選項?
- 13. 如何以編程方式使UICollectionView填充UITableViewCell?
- 14. 如何以編程方式用CoreData行填充NSArrayController?
- 15. 用複選框以編程方式填充首選項
- 16. 以編程方式在WPF中用文本框填充網格
- 17. 以編程方式確定分組的UITableView的填充
- 18. 以編程方式填充共享點中的選擇字段
- 19. Android:以編程方式設置textview的一個填充
- 20. javascript - 以編程方式計算元素之間的填充
- 21. 以編程方式填充的不刷新屏幕TableView
- 22. 如何以編程方式填充React的輸入字段值?
- 23. 如何以編程方式填充使用React構建的輸入元素?
- 24. 以奇妙的方式填充d3樣式填充
- 25. Swift Realm,以正確的方式加載預填充數據庫?
- 26. Android:以編程方式從Java代碼填充微調器
- 27. 如何以編程方式填充數據庫
- 28. 4x4 GridLayout在Android中以編程方式填充
- 29. Perl Curses :: UI - 以編程方式填充Buttonbox
- 30. DX11以編程方式填充3D紋理