Swift 不能通过委托调用协议方法

Swift can#39;t call protocol method via delegate(Swift 不能通过委托调用协议方法)
本文介绍了Swift 不能通过委托调用协议方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两节课.一个类名为 ViewController,另一个类名为 TabView.

I have two classes. One class is named ViewController and the other class is named TabView.

我的目标是从 ViewController 调用 TabView 类中的函数 changeTab().

My goal is to call a function changeTab() which is inside the TabView class from the ViewController.

不知何故,我遇到了麻烦,因为每次我的委托都是 nil.

Somehow I am having trouble with it because everytime my delegate is 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")
    }
}

谁能解释我做错了什么?- 我是代表和协议的新手...

Can someone explain me what I am doing wrong? - I am new to delegates and protocols...

推荐答案

你错误地使用了委托模式.很难说您想为哪个控制器定义协议以及您想采用哪个控制器 - 但这是一种可能的方式.

You are using the delegate pattern wrongly. It is hard to tell which controller you want to define the protocol for and which one you want to adopt it - but here is one possible way.

// 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")
    }
}

这篇关于Swift 不能通过委托调用协议方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)