Use of Unimplemented Initializer 'Init(Frame:)' for Class When Instantiating a Subclass of Uisegementedcontrol

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



Leave a reply



Submit