我必须将手势识别器的单一逻辑应用于所有 UIViewController
我的应用程序中的元素。
我想创建一个基类。然而,据我所知,我们应该只在非常必要的情况下才应该在继承链中添加额外的层。
将我上面提到的功能添加到我的项目中的最有效方法是什么,而无需将代码复制粘贴到每个我的 Controller ?
最佳答案
您可以使用 UIViewController
的扩展名
extension UIViewController
{
// write code that has to be used in all view controllers
func applyTheRequiredPropertiesToGestureRecognizer(gr:UIGestureRecognizer)
{
// do whatever you want to do
}
}
稍后在使用时
class SomeViewController:UIViewController{
let gestureRecog = UIGestureRecognizer()
//call the method
func someMethod()
{
self.applyTheRequiredPropertiesToGestureRecognizer(gestureRecog)
}
}
关于ios - UIViewController 基类——好不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254972/