我有一个UISegmentedControl的子类,在Cocoa Touch类中称为SegControl
。
class SegControl: UISegmentedControl {
override var = "test"
}
现在我的ViewController中有一个UISegmentControl我该怎么做才符合我的自定义类?
编辑: 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
}