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

时间:2018-11-16 02:29:02

标签: ios swift delegates nsnotificationcenter

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

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方法选择传递数据,则很有必要。

我想知道ViewControllers之间的哪个传递数据选项在这种情况下会更好,因为通常在示例中说明了方法,其中两个视图控制器通过segue连接,但是在我的情况下,屏幕的流动是异常。谢谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

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
    }

}

答案 1 :(得分:-1)

对。我建议在loginPopupVC中声明一个变量,以包含对ThirdViewController的引用,因此在实例化loginVC之后,在tabBar:didSelect中将其传递给ThirdVC的引用,但在呈现登录名之前。在Login中,该变量是类级别的,因此您可以从updateUI函数访问它。现在,在ThirdView中声明一个方法或变量以包含json并将其传递。