UIViewController 扩展不允许覆盖 Swift 中的视图相关功能?
UIViewController extension not allowing to override view related functions in Swift?
在尝试为 UIViewController 实现扩展时,我意识到没有正常的方法,或者不允许覆盖此功能(即使它们可用于 UICollectionViewController 和 UITableViewController):
extension UIViewController{
public override func viewWillAppear(){
super.viewWillAppear()
//do some stuff
}
}
我意识到没有正常的方法,或者不允许重写这个函数(即使它们可用于 UICollectionViewController 和 UITableViewController):
- viewDidLoad
- viewWillLoad
- viewWillAppear
- viewDidAppear
有什么方法可以做到这一点?我想在那里有一些实现,并为我的应用程序上的每个 UIViewController 工作......所有这些都在一个地方。
Please, note that I don't want to make a new class subclassing
UIViewController, overriding those methods and making my controller to
extend it. This is the obvious and simplest solution, but this do not satisfy what I'm trying to do.
I'm using swift 1.2 in XCode 6.3
您尝试执行的操作与此代码执行的操作类似:
class MyClass {
func myFunc() {}
}
extension MyClass {
override func myFunc() {}
}
您试图重写的 4 个方法在 UIViewController
中定义,而不是其超class 之一。而且您不能覆盖在相同 class.
中定义的方法
更新
我可以想到 2 种不同的方法来解决问题 - 第一种是你不想要的 (subclassing UIViewController
)。
另一个是 method swizzling - 我从未使用过它,所以我不想向您提供不准确的信息。也许值得一读 Nate Cook 的 this article,它顺便展示了一个替换 viewWillAppear
.
的例子
在尝试为 UIViewController 实现扩展时,我意识到没有正常的方法,或者不允许覆盖此功能(即使它们可用于 UICollectionViewController 和 UITableViewController):
extension UIViewController{
public override func viewWillAppear(){
super.viewWillAppear()
//do some stuff
}
}
我意识到没有正常的方法,或者不允许重写这个函数(即使它们可用于 UICollectionViewController 和 UITableViewController):
- viewDidLoad
- viewWillLoad
- viewWillAppear
- viewDidAppear
有什么方法可以做到这一点?我想在那里有一些实现,并为我的应用程序上的每个 UIViewController 工作......所有这些都在一个地方。
Please, note that I don't want to make a new class subclassing UIViewController, overriding those methods and making my controller to extend it. This is the obvious and simplest solution, but this do not satisfy what I'm trying to do.
I'm using swift 1.2 in XCode 6.3
您尝试执行的操作与此代码执行的操作类似:
class MyClass {
func myFunc() {}
}
extension MyClass {
override func myFunc() {}
}
您试图重写的 4 个方法在 UIViewController
中定义,而不是其超class 之一。而且您不能覆盖在相同 class.
更新
我可以想到 2 种不同的方法来解决问题 - 第一种是你不想要的 (subclassing UIViewController
)。
另一个是 method swizzling - 我从未使用过它,所以我不想向您提供不准确的信息。也许值得一读 Nate Cook 的 this article,它顺便展示了一个替换 viewWillAppear
.