ios - 在不相关的 ViewController 之间传递数据

标签 ios swift delegates nsnotificationcenter

我正在尝试实现一个将数据放入 View Controller 的登录屏幕,通过覆盖 tabBarController didSelect 方法调用登录 View Controller ,如下所示:

enter image description here

请注意,TabBarController 和 ThirdViewController 之间没有任何连接,因为我按如下方式重写了 tabBarController:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {

// This delegate open the modal view after open the desired view.
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController is MyThirdViewController {
            if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") {
                tabBarController.present(loginPopupVC, animated: true)
            }
        }
    }
}

现在,在我的 LoginViewController 中,我用应该填充 ThirdViewController 的数据(姓名、性别、出生等)解析结构 (LoginResponse) 中的 JSON。我已经在 LoginViewController 中使用此代码段执行此操作:

struct LoginResponse : Decodable {
    var name: String
    var surname: String
    var sex: String
    var birth: String
}

class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate {

@IBAction func cancelLogin(_ sender: UIButton) {
        //LoginViewController will close and ThirdViewController will open
        dismiss(animated: true, completion: nil)
    }

@IBAction func makeLogin(_ sender: UIButton) {
        //LoginViewController brings data, closing itself and opening ThirdViewController
        self.updateUI()
        self.dismiss(animated: true, completion: nil)
    }

func updateUI() {
        do {
            let jsonDecoder = JSONDecoder()
            let myjson = "{\"name\": \"MyName\", \"surname\": \"MySurname\", \"sex\": \"Male\", \"birth\": \"1980-05-15\"}"
            let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
        }
        catch let jsonErr{
            print(jsonErr)
        }

    }
}

现在,我想将该数据(在 loginResult 内)传递给 ThirdViewController。

我想我不能从 LoginViewController 调用 ThirdViewController,因为 TabBarController 已经这样做了,如果我选择使用 Delegate 方法或 NotificationCenter 方法传递数据,这是必需的。

我想知道在这种情况下,ViewController 之间的哪些传递数据选项会更好,因为通常这些方法在示例中用两个 View Controller 通过一个 segue 连接来解释,但在我的例子中,屏幕的流程是异常。谢谢。

最佳答案

你可以试试这样的:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UITabBarControllerDelegate {

    // This delegate open the modal view after open the desired view.
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if viewController is MyThirdViewController {
            if let loginPopupVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") {
                loginPopupVC.delegate = tabBarController
                tabBarController.present(loginPopupVC, animated: true)
            }
        }
    }
}

对于 loginPopupVC:

struct LoginResponse : Decodable {
    var name: String
    var surname: String
    var sex: String
    var birth: String
}

class LoginViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate {

    var delegate: loginDelegate?

    @IBAction func cancelLogin(_ sender: UIButton) {
        //LoginViewController will close and ThirdViewController will open
        dismiss(animated: true, completion: nil)
    }

    @IBAction func makeLogin(_ sender: UIButton) {
        //LoginViewController brings data, closing itself and opening ThirdViewController
        self.updateUI()
        self.dismiss(animated: true, completion: nil)
    }

    func updateUI() {
        do {
            let jsonDecoder = JSONDecoder()
            let myjson = "{\"name\": \"MyName\", \"surname\": \"MySurname\", \"sex\": \"Male\", \"birth\": \"1980-05-15\"}"
            let loginResult = try jsonDecoder.decode(LoginResponse.self, from: Data(myjson.utf8))
            // Pass data using delegate
            delegate?.handleLogin(with: loginResult)

        }
        catch let jsonErr{
            print(jsonErr)
        }

    }
}

然后对于您的 tabBarController 类:

protocol loginDelegate: class {
    func handleLogin(with object: LoginResponse)
}

class myTabBarController: UITabBarController, loginDelegate {

    // regular tabBarController lifecycle methods

    func handleLogin(with object: LoginResponse) {
        // do work with data
    }

}

关于ios - 在不相关的 ViewController 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53330598/

相关文章:

iphone - iOS 中的 StoreKit - 自动续订订阅 - 如何知道它是否是免费试用版?

ios - AudioUnit Mixer Host 应用程序中的 AudioUnitSampleType

swift - 如何切换数据库?

c# - 通用 Property.GetSetMethod 的委托(delegate)

ios - 没有已知的选择器实例方法——委托(delegate)问题?

ios - 如何在 UI 线程中运行代码,从其他线程调用它

ios - 如何将 UIWebView 垂直滚动事件传递给父 View ?

ios - 给 UIBarButton 添加阴影

ios - Swift 在协议(protocol)中声明类函数

c++ - C++ 中的委托(delegate)(或类似的东西)