简体   繁体   中英

Use UIViewController class on UITableViewController

I have a Swift class that inherits from UIViewController, and I was wondering if there was any way that I could use the same class on a UITableViewController to avoid repetition.

class GradientViewController: UIViewController {
    // My class
}

Are there any ways to use this class on a UITableViewController, like so?

class MyTableViewController: GradientViewController {
    // My table view controller
}

Thanks in advance

Swift only supports single inheritance.

You could use a protocol to avoid repetition.

protocol YourProtocolName {
    // Protocol stuff
}

class GradientViewController: UIViewController, YourProtocolName {
    // Implement protocol requirements
}

class MyTableViewController: UITableViewController, YourProtocolName {
    // Implement protocol requirements
}

If it makes sense, you could even use a protocol extension to provide default implementations such that conforming types don't need to add any code to conform.

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