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

Answers


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.


Need Your Help

avqueueplayer play in background mode issue

ios background-process avqueueplayer

I use avqueueplayer to play some very short audios. Once an item is completed, it shall pause for 2 second and then play the next item.