Use of unimplemented initializer 'init(frame:)' for class when instantiating a subclass of UISegementedControl
You could call super.init(frame
and insert the segments manually.
And you have to add a target
parameter to the custom init(actionName
method.
class MySegmentControl: UISegmentedControl {
init(actionName: Selector, target: Any?) {
super.init(frame: .zero)
insertSegment(withTitle: "Two", at: 0, animated: false)
insertSegment(withTitle: "One", at: 0, animated: false)
self.selectedSegmentIndex = 0
self.layer.cornerRadius = 5.0
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.blue.cgColor
self.addTarget(target, action: actionName, for: .valueChanged)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Fatal error: Use of unimplemented initializer 'init(frame:textContainer:) when instantiating a subclass of NSTextView
Your subclass needs to call the designated initializer of the parent.
Change super.init(frame: frame)
to super.init(frame: frame, textContainer: nil)
.
Error setting `items` and target action when subclassing UISegmentControl
First ERROR:
Don't call init(frame
Second ERROR:
actionName
is already a Selector
class MySegmentControl: UISegmentedControl {
init(actionName: Selector) {
let discountItems = ["One" , "Two"]
super.init(items: discountItems)
self.selectedSegmentIndex = 0
self.layer.cornerRadius = 5.0
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.blue.cgColor
self.addTarget(self, action: actionName, for: .valueChanged)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Error: Unrecognised selector sent to instance when tap on UISegmentedControl
You must be a newbie!
You should remove self.addTarget(self, action: self.actionName, for: .valueChanged)
in class MySegmentControl
, add added the below the function viewDidLoad()
:
segmentOne.addTarget(self, action: #selector(segmentAction(sender:)), for: .valueChanged)
Cannot subclass UIButton: Must call a designated initializer of the superclass 'UIButton'
You have to call the superclass' designated initializer:
Swift 3 & 4:
init(letter: String, viewController: CustomViewController) {
super.init(frame: .zero)
}
Swift 1 & 2:
init(letter: String, viewController: CustomViewController) {
super.init(frame: CGRectZero)
}
As Paulw11 says in the comments, a view generally shouldn't have a reference to its controller, except as a weak reference using the delegate pattern, which would promote reusability.
Related Topics
API to Capture Live Photos in iOS9
Core Data Taking Time to Insert Records with Fetching Entity & Set as Relationship
How Is This Slide-Up Menu from the iPhone Messages App Implemented
Returning Data from Function in Firebase Observer Code Block Swift
String to Double in Xcode 6's Swift
Swiftui How Add Custom Modifier with Callback
How to Use " Let Newswiftcolor = Uicolor(Red: 255, Green: 165, Blue: 0, Alpha: 0)
Swift Extension and Enum for Color Schemes
Daily Local Notifications Are Not Working
Uibutton Subclass @Ibaction Not Working
Buttonwithtype' Is Unavailable: Use Object Construction 'Uibutton(Type:)
Setting a Stateobject Value from Child View Causes Navigationview to Pop All Views
How to Use Nsuserdefaults to Store an Array of Custom Classes in Swift
Prevent Uitableview Scrolling Below a Certain Point
iOS Swift - Custom Camera Overlay
iOS 10 Issue: Uiscrollview Not Scrolling, Even When Contentsize Is Set