【问题标题】:How to receive same callback in two ViewControllers when one is opened?打开一个 ViewController 时,如何在两个 ViewController 中接收相同的回调?
【发布时间】:2019-04-23 21:30:00
【问题描述】:

我想在我的 Swift 应用程序中服务器响应时打开的 ViewController 中接收相同的回调。

我有两个 ViewController。第一个 ViewController 从“NetworkService”类注册一个回调。

第二个 ViewController 从第一个 ViewController 打开,第二个从变量中初始化的 firstViewController 接收“NetworkService”,然后注册相同的回调。

当我尝试从服务器接收回调时,如果第一个 ViewController 被打开,我会收到响应。如果我打开第二个 ViewController 并重新发送响应,我会在第二个 ViewController 中正确地得到它。

但是,如果我返回第一个 ViewController 并收到响应,则它始终只会在第二个 ViewController 上收到。

class NetworkService {

    var onFunction: ((_ result: String)->())?

    func doCall() {
        self.onFunction?("result")
    }

}


class MyViewController: UIViewController {

    let networkService = NetworkService()

    override func viewDidLoad() {
        super.viewDidLoad()

        networkService.onFunction = { result in
            print("I got \(result) from the server!")
        }

    }
}

我打开 secondViewController 喜欢:

let vc = self.storyboard!.instantiateViewController(withIdentifier: "second") as! SecondViewController
vc. networkService = networkService
        self.navigationController?.pushViewController(vc, animated: true)

还有第二个 ViewController:

class SecondViewController: UIViewController {

    var networkService: NetworkService?

    override func viewDidLoad() {
        super.viewDidLoad()

        networkService!.onFunction = { result in
            print("I got \(result) from the server!")
        }

    }
}

如何在第一个 ViewController 中再次接收响应,然后从调用 popViewController 的第二个返回到第一个 ViewController?

self.navigationController?.popViewController(animated: false)  

【问题讨论】:

  • 你为什么不尝试NSNotification 而不是回调。这样,无论您身在何处,您都会收到该通知的所有观察者的回调。

标签: ios swift uiviewcontroller callback popviewcontroller


【解决方案1】:

如何在两个 ViewController 上调用 viewDidAppear 中的函数,以便每次在两个视图之间切换时都能得到响应?您不需要在 ViewController 之间传递 networkService

override func viewDidAppear(_ animated: Bool) {

  networkService!.onFunction = { result in
            print("I got \(result) from the server!")
        }

}

【讨论】:

    【解决方案2】:

    您可以使用通知,但您必须在视图之间切换时注册和取消注册 VC。其他选项是使用委托,您将需要共享 NetworkService 实例。这是如何与协议一起工作的快速示例。

    protocol NetworkServiceProtocol {
        var service: NetworkService? { get }
        func onFunction(_ result: String)
    }
    
    class NetworkService {
    
        var delegate: NetworkServiceProtocol?
    
        func doCall() {
            self.delegate?.onFunction("results")
        }
    
        func update(delegate: NetworkServiceProtocol) {
            self.delegate = delegate
        }
    }
    
    class VC1: UIViewController, NetworkServiceProtocol {
        var service: NetworkService?
    
        init(service: NetworkService? = nil) {
            self.service = service
            super.init(nibName: nil, bundle: nil)
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            self.service?.update(delegate: self)
        }
    
        func onFunction(_ result: String) {
            print("On Function")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      相关资源
      最近更新 更多