R.Tech R.Tech - 1 month ago 15
Swift Question

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

I have a subclass for a UISegmentedControl which is called

SegControl
in a Cocoa Touch Class.

class SegControl: UISegmentedControl {
override var = "test"
}


And now I have a UISegmentControl in my ViewController what do I have to do that this conforms to my custom class?

EDIT:
Full code for a Reselectable UISegmentControl for Swift 3

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
}

Answer

Your question isn't entirely clear but to start you need to replace any references to UISegmentedControl with SegControl. This includes updating any references in code as well as changing the class name of the object in any storyboard or xib.

You are also going to need to add the proper init methods to your subclass so you can create instances of it.

And of course you need to added whatever additional code that makes having this subclass useful.