简体   繁体   中英

Change subview class in subclass

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()
        }
    }
}

I guess that you add button into view hierarchy in View . If so, button = CircleButton() is meaningless. However, you still can do that way by remove button in OtherView , then add it again.

class View: UITableViewCell {
    var button: GenericButton

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        button = GenericButton()
        addButton(button)
    }

    func addButton(button: UIButton) {
        // add button into view hierarchy here
    }
}

class OtherView: View {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        button.removeFromSuperview()
        button = CircleButton()
        addButton(button)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM