在Swift中访问UISegementedControl

时间:2014-07-24 18:58:53

标签: ios swift uisegmentedcontrol

我一直在玩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

4 个答案:

答案 0 :(得分:14)

以下是我使用过的并且对我有用 -

@IBOutlet var mysegmentedControl : UISegmentedControl?

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@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()
    println("# of Segments = \(mysegmentedControl?.numberOfSegments)")

}

答案 1 :(得分:1)

请使用swift查找以下代码片段以访问uisegmentedcontrol。

   @IBOutlet weak var segmentedControl: UISegmentedControl!

    @IBOutlet weak var textLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textLabel.text = "First Segment Selected";
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func segmentedControlAction(sender: AnyObject) {

        if(segmentedControl.selectedSegmentIndex == 0)
        {
            textLabel.text = "First Segment Selected";
        }
        else if(segmentedControl.selectedSegmentIndex == 1)
        {
            textLabel.text = "Second Segment Selected";
        }
        else if(segmentedControl.selectedSegmentIndex == 2)
        {
            textLabel.text = "Third Segment Selected";
        }
    }

如需详细说明请参考以下链接。

http://sourcefreeze.com/uisegmentedcontrol-example-using-swift-in-ios/

答案 2 :(得分:0)

我能够成功编译和运行您的代码。你的问题不是你的分段ctrl,可能你的一个可选对象是nil,因此你无法解开它。看看你的可选对象。

另外,试试         @IBOutlet var mysegmentedControl:UISegmentedControl?

这绝对可以解决你的问题

答案 3 :(得分:0)

这就是IBOutlet的声明方式。实际上,Xcode 6 Beta 4会自动为所有IBOutlet执行此操作。的!将变量标记为Implicitly Unwrapped Optional:

@IBOutlet var mysegmentedControl : UISegmentedControl!

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html

阅读“隐式解包的可选类型”部分。