Swift 无法通过委托调用协议方法
Swift can't call protocol method via delegate
我有两个 class。一个 class 被命名为 ViewController
而另一个 class 被命名为 TabView
.
我的目标是从 ViewController.
调用 TabView class 内的函数 changeTab()
不知何故我遇到了麻烦,因为每次我的代表都是 nil
。
这是我的 ViewController 代码:
protocol TabViewProtocol: class {
func changeTab()
}
class ViewController: NSViewController {
// delegate
weak var delegateCustom : TabViewProtocol?
override func viewDidLoad() {
print(delegateCustom) // outputs "nil"
}
buttonClickFunction() {
print(delegateCustom) // outputs "nil"
delegateCustom?.changeTab() // doesn't work
}
}
这是我的 TabView 代码:
class TabView: NSTabViewController, TabViewProtocol {
let myVC = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
myVC.delegateCustom = self
}
func changeTab() {
print("test succeed")
}
}
有人可以解释一下我做错了什么吗? - 我是新来的代表和协议...
您错误地使用了委托模式。很难说出您要为哪个控制器定义协议以及要采用哪个控制器 - 但这是一种可能的方法。
// 1. Define your protocol in the same class file as delegate property.
protocol TabViewProtocol: class {
func changeTab()
}
// 2. Define your delegate property
class ViewController: NSViewController {
// delegate
weak var delegateCustom : TabViewProtocol?
override func viewDidLoad() {
// It should be nil as you have not set the delegate yet.
print(delegateCustom) // outputs "nil"
}
func buttonClickFunction() {
print(delegateCustom) // outputs "nil"
delegateCustom?.changeTab() // doesn't work
}
}
// 3. In the class that will use the protocol add it to the class definition statement
class TabView: NSTabViewController, TabViewProtocol {
let myVC = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
myVC.delegateCustom = self
// Should output a value now
print(myVC.delegateCustom) // outputs "self"
}
func changeTab() {
print("test succeed")
}
}
您正在这一行中创建一个新实例:
let myVC = ViewController()
您应该获取 ViewController.then 集
的现有实例
myVC.delegateCustom = self
我有两个 class。一个 class 被命名为 ViewController
而另一个 class 被命名为 TabView
.
我的目标是从 ViewController.
调用 TabView class 内的函数changeTab()
不知何故我遇到了麻烦,因为每次我的代表都是 nil
。
这是我的 ViewController 代码:
protocol TabViewProtocol: class {
func changeTab()
}
class ViewController: NSViewController {
// delegate
weak var delegateCustom : TabViewProtocol?
override func viewDidLoad() {
print(delegateCustom) // outputs "nil"
}
buttonClickFunction() {
print(delegateCustom) // outputs "nil"
delegateCustom?.changeTab() // doesn't work
}
}
这是我的 TabView 代码:
class TabView: NSTabViewController, TabViewProtocol {
let myVC = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
myVC.delegateCustom = self
}
func changeTab() {
print("test succeed")
}
}
有人可以解释一下我做错了什么吗? - 我是新来的代表和协议...
您错误地使用了委托模式。很难说出您要为哪个控制器定义协议以及要采用哪个控制器 - 但这是一种可能的方法。
// 1. Define your protocol in the same class file as delegate property.
protocol TabViewProtocol: class {
func changeTab()
}
// 2. Define your delegate property
class ViewController: NSViewController {
// delegate
weak var delegateCustom : TabViewProtocol?
override func viewDidLoad() {
// It should be nil as you have not set the delegate yet.
print(delegateCustom) // outputs "nil"
}
func buttonClickFunction() {
print(delegateCustom) // outputs "nil"
delegateCustom?.changeTab() // doesn't work
}
}
// 3. In the class that will use the protocol add it to the class definition statement
class TabView: NSTabViewController, TabViewProtocol {
let myVC = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
myVC.delegateCustom = self
// Should output a value now
print(myVC.delegateCustom) // outputs "self"
}
func changeTab() {
print("test succeed")
}
}
您正在这一行中创建一个新实例:
let myVC = ViewController()
您应该获取 ViewController.then 集
的现有实例myVC.delegateCustom = self