Let's say I have a UITableViewCell class called custom view with a button of class GenericButton, which has been defined in auto layout programmatically
class View: UITableViewCell {
var button: GenericButton
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
button = GenericButton()
// blah blah blah constraints
}
}
Now let's say I have a subclass of View called OtherView, and I want that button to become a subclass of GenericButton called CircleButton.
class OtherView: View {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
button = CircleButton()
}
}
However, my view still shows a GenericButton, not a CircleButton. What am I doing wrong?
Ok I've thought about this for several hours and got the conclusion that this is the possibly the best answer.
import UIKit
class View: UITableViewCell
{
var button: GenericButton! = nil
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
convenience init(button: Bool, style: UITableViewCellStyle, reuseIdentifier: String?)
{
self.init(style: style, reuseIdentifier: reuseIdentifier)
if button
{
button = GenericButton()
setConstraint()
}
}
private func setConstraint()
{
// blah blah blah constraints
}
}
class OtherView: View
{
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
convenience init(button: Bool, style: UITableViewCellStyle, reuseIdentifier: String?)
{
self.init(style: buttonWithStyle, reuseIdentifier: reuseIdentifier)
if button
{
button = CircleButton()
setConstraint()
}
}
}