【发布时间】:2014-09-16 11:44:13
【问题描述】:
我一直在使用 UISegmentedControl 并希望根据需要对其进行管理。我在故事板上创建了它,连接了出口和动作。它可以很好地可视化,并且操作 indexchanged 上的回调有效。我想在可视化之前对此进行一些其他管理,所以我被困在如何正确访问我在情节提要中设置的 mysegmentedControl 上。似乎是一个愚蠢的问题,但我找不到当前可以涉及的 UISegmentedControl 的 Swift 示例。
class ViewController: UIViewController, MKMapViewDelegate {
...
//playing around with a Segmented Control
@IBOutlet var mysegmentedControl : UISegmentedControl
// Any time the button is clicked in the Segmented Control the index changes so we catch it to do something.
@IBAction func indexChanged(sender : UISegmentedControl) {
// This all works fine and it prints out the value of 3 on any click
println("# of Segments = \(sender.numberOfSegments)")
switch sender.selectedSegmentIndex {
case 0:
println("first segement clicked")
case 1:
println("second segment clicked")
case 2:
println("third segemnet clicked")
default:
break;
} //Switch
} // indexChanged for the Segmented Control
override func viewDidLoad() {
super.viewDidLoad()
// 1. How do I get proper access to the mysegmentedControl that was created in the storyboard and is already displayed.
// The indexChanged function gets called fine on the display via sender getting passed to it.
// I equate this to getting the pointer to it in C so I leverage it elsewhere
// 2. Guessing I don't need to init a new UISegmentedControl or do I?
// This compiles but crashes on run with: fatal error: Can't unwrap
println("# of Segments = \(mysegmentedControl.numberOfSegments)")
} // viewDidLoad
} // ViewController
【问题讨论】:
标签: ios swift uisegmentedcontrol