我在哪里调用子类? (可重新选择的 UISegmentedControl Swift 3)

Where do I call the Subclass? (reselectable UISegmentControl Swift 3)

我有一个 UISegmentedControl 的子class,在 Cocoa Touch Class.

中称为 SegControl
class SegControl: UISegmentedControl {
override var = "test"
}

现在我的 ViewController 中有一个 UISegmentControl,我必须怎么做才能符合我的习惯 class?

编辑: Swift 3

的可重选 UISegmentControl 的完整代码
class ReselectableSegmentedControl: UISegmentedControl {

    @IBInspectable var allowReselection: Bool = true
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let previousSelectedSegmentIndex = self.selectedSegmentIndex
        super.touchesEnded(touches, with: event)
        if allowReselection && previousSelectedSegmentIndex == self.selectedSegmentIndex {
            if let touch = touches.first {
                let touchLocation = touch.location(in: self)
                if bounds.contains(touchLocation) {
                    self.sendActions(for: .valueChanged)
                }
            }
        }

    }
}

UISegmentControl:

var savedSegment: Int = -1

    @IBAction func segmentChanged(_ sender: ReselectableSegmentedControl) {

        if (sender.selectedSegmentIndex == savedSegment) {
            sender.selectedSegmentIndex =  UISegmentedControlNoSegment
            savedSegment = UISegmentedControlNoSegment
        }
        else {
            savedSegment = sender.selectedSegmentIndex
        }

您的问题并不完全清楚,但首先您需要将对 UISegmentedControl 的任何引用替换为 SegControl。这包括更新代码中的任何引用以及更改任何故事板或 xib 中对象的 class 名称。

您还需要将适当的 init 方法添加到您的子程序class,以便您可以创建它的实例。

当然,您需要添加使此子class有用的任何额外代码。

在布局构建器中添加默认的 UISegmentControl

然后单击它并在身份检查器下的侧面菜单中 -> class 写下你的 class 名字 SegControl