简体   繁体   English

在UITableViewController上使用UIViewController类

[英]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.

相关问题 如果IBOutlet在UIViewController类中,则如何访问UITableViewController类中的IBOutlet - How to access IBOutlet in UITableViewController Class if the IBOutlet is in UIViewController Class 我可以让UITableViewController和UIViewController从同一个类继承吗? - Can I have UITableViewController and UIViewController inherit from the same class? 是否可以使用故事板使用UIViewController而不是UITableViewController创建静态表? - Is it possible to use storyboards to create a static table using UIViewController instead of UITableViewController? UITableViewController与UIViewController - UITableViewController vs UIViewController 检查superview是否是UITableViewController UIViewController? - Check if superview is UITableViewController UIViewController? 将UIViewController更改为UITableViewController - Changing UIViewController to UITableViewController UITableViewController和UIViewController中的UIPageViewController - UITableViewController with UIPageViewController in UIViewController 从UIViewController选择到UITableViewController - segue from UIViewController to UITableViewController UIViewController和UITableViewController-设置页面 - UIViewController & UITableViewController - Settings Page 在UITableViewController IOS中扩展UIViewController - Extend UIViewController in UITableViewController IOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM