[英]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. 我有一个从UIViewController继承的Swift类,我想知道是否有什么方法可以在UITableViewController上使用同一类以避免重复。
class GradientViewController: UIViewController {
// My class
}
Are there any ways to use this class on a UITableViewController, like so? 有没有办法像这样在UITableViewController上使用此类?
class MyTableViewController: GradientViewController {
// My table view controller
}
Thanks in advance 提前致谢
Swift only supports single inheritance. Swift仅支持单一继承。
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. 如果有道理,您甚至可以使用协议扩展来提供默认的实现,从而使符合类型不需要添加任何代码即可符合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.