Xcode UISegmentedControl选定段始终为-1

Xcode UISegmentedControl选定段始终为-1,xcode,swift,uisegmentedcontrol,Xcode,Swift,Uisegmentedcontrol,我将UISegmentedControl子类化,以触发valueChanged方法,该方法工作正常。现在,当我选择一个段时,索引总是-1。 我在Xcode 6 beta 4上,我找不到我做错了什么。 子类: import UIKit class MySegCotrl: UISegmentedControl { init(coder aDecoder: NSCoder!) { super.init(coder: aDecoder) } override func touchesEn

我将UISegmentedControl子类化,以触发valueChanged方法,该方法工作正常。现在,当我选择一个段时,索引总是-1。 我在Xcode 6 beta 4上,我找不到我做错了什么。 子类:

import UIKit

class MySegCotrl: UISegmentedControl {

init(coder aDecoder: NSCoder!)  {
    super.init(coder: aDecoder)
}

override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
    let current = self.selectedSegmentIndex
    super.touchesBegan(touches, withEvent: event)
    println("Current:\(current)")
    println("Self.selectedSegmentIndex:\(self.selectedSegmentIndex)")
    if current == self.selectedSegmentIndex {
        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
    }
}

}
my UIViewController类中的valueChanged方法

@IBOutlet var segCotrRelevanz: MySegCotrl!

@IBAction func relevanChanged(sender: AnyObject) {

    println("sender.selectedSegmentIndex:\(sender.selectedSegmentIndex)")
    println("segCotrRelevanz.selectedSegmentIndex:\(segCotrRelevanz.selectedSegmentIndex)")

    if sender.selectedSegmentIndex == segCotrRelevanz.selectedSegmentIndex {
        println("Same")
    }

}
输出:

Current:-1
Self.selectedSegmentIndex:-1
sender.selectedSegmentIndex:-1
segCotrRelevanz.selectedSegmentIndex:-1
Same

尝试将方法更改为touchesbreated而不是touchesend,保持原样:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
    let current = self.selectedSegmentIndex
    super.touchesBegan(touches, withEvent: event)
    println("Current:\(current)")
    println("Self.selectedSegmentIndex:\(self.selectedSegmentIndex)")
    if current == self.selectedSegmentIndex {
        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
    }
}

问题是,即使我需要调用super.touchesend,我还是调用super.touchesend。您的解决方案适用于iOS 6及更低版本,谢谢!