通过委托将数据传递给各种视图控制器

Passing data to various view controllers via delegate

努力学习通过 delegates 传递数据的基础知识。我正在尝试将一个字符串从我的 viewController 传递到我的 viewController2 并打印它。我收到错误:

"Type ViewController2 has no member delagate" in my view controller 2.

我想不通哪里出错了。

viewController 1:

protocol datadelagate {
    func printThisString(string: String)
}

class ViewController: UIViewController {

    var delegate: datadelagate?

    override func viewDidLoad() {
        delegate?.printThisString(string: "This was passed from first controller to second controller")
    }
}

这是我的 viewController 2:

class ViewController2: UIViewController, datadelagate {

    func printThisString(string: String) {
        print(string)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ViewController2.delagate = self
        print(String.self)
    }
}

如果您希望 ViewController2ViewController 打印一些值,您可能必须这样做:

protocol datadelagate {
    func printThisString(string: String)
    func getStringFromVC1() -> String
}

class ViewController: UIViewController, datadelagate {

    let someString: String = "From VC1"

    func printThisString(string: String) {
        print(string)
    }

    func getStringFromVC1() -> String {
        return someString
    }

    override func viewDidLoad() {
        ViewController2.delagate = self

    }
}
class ViewController2: UIViewController {

    var delegate: datadelagate?

    override func viewDidLoad() {
        super.viewDidLoad()

        //This is how something from VC2 is sent to VC1's scope.
        delegate?.printThisString(string: "Calling the delegate to print something from ViewController2 on first ViewController")

        //The below call gets you some value from VC1. (This is what you wanted, I belive...)
        print(delegate?.getStringFromVC1())
    }
}

现在进行一些解释:

为了简单理解,假设 delegate 是一个从事特定工作的人 (protocol)。

你有一个“代表”

您要求您的代表与您的朋友一起工作,您的朋友承认了。 (通过 You.delegate = self 分配您的代表,其中 self 是您的朋友)

现在,通过您的 delegate,您可以与您的朋友一起做一些事情,方法是让您的 delegate 做一些工作(在 protocol 中定义)。


编辑

上面的代码将不起作用,因为非静态数据成员试图在不创建实例的情况下访问


工作代码

import UIKit

class ViewController2: UIViewController {

    static let sharedInstance = ViewController2()
    weak var delegate: DataDelagate?

    override func viewDidLoad() {
        super.viewDidLoad()
        //This is how something from VC2 is sent to VC1's scope.
        delegate?.printThis(string: "Calling the delegate to print something from ViewController2 on first ViewController")

        //The below call gets you some value from VC1. (This is what you wanted, I belive...)
        print(delegate?.getStringFromVC1() ?? "s")
    }
}

class ViewController: UIViewController {

    static let sharedInstance = ViewController2()
    var someString: String = "From VC1"

    override func viewDidLoad() {
        super.viewDidLoad()
        ViewController2.sharedInstance.delegate = self
    }
}
extension ViewController: DataDelagate {
    func printThis(string: String) {
        print(string)
    }

    func getStringFromVC1() -> String {
        return someString
    }
}

protocol DataDelagate: AnyObject {
    func printThis(string: String)
    func getStringFromVC1() -> String
}