Ios 切换选项卡时向控制器发送数据

Ios 切换选项卡时向控制器发送数据,ios,swift,xcode,swift4,Ios,Swift,Xcode,Swift4,我的应用程序有4个标签。在第四个选项卡上,有一个“登录”应用程序的选项,它将用户带到登录控制器。当用户登录时,我想将用户切换回第一个选项卡,我使用以下代码执行此操作: self.tabBarController?.selectedIndex = 0 但是,我想在用户登录后刷新第一个选项卡上的内容(因为内容是特定于用户的)。这意味着,当用户切换选项卡时,我需要传递一些数据,以便让第一个选项卡中的控制器知道如何刷新数据 在选项卡之间切换以刷新内容时,如何将数据发送到第一个选项卡?还是有更好的解决方

我的应用程序有4个标签。在第四个选项卡上,有一个“登录”应用程序的选项,它将用户带到登录控制器。当用户登录时,我想将用户切换回第一个选项卡,我使用以下代码执行此操作:

self.tabBarController?.selectedIndex = 0
但是,我想在用户登录后刷新第一个选项卡上的内容(因为内容是特定于用户的)。这意味着,当用户切换选项卡时,我需要传递一些数据,以便让第一个选项卡中的控制器知道如何刷新数据

在选项卡之间切换以刷新内容时,如何将数据发送到第一个选项卡?还是有更好的解决方法?

您可以通过使用通知/观察者或委托模式来完成

因为它似乎是一对一关系,所以您可能应该使用委托模式,因为通知和观察者用于一对多关系

  • 在第四个选项卡视图控制器代码的正上方创建协议。此函数的参数将是从一个ViewController传输到另一个ViewController的数据。在本例中,我传输了文本字段的文本,用户可以在其中输入其名称:

    protocol SelectionDelegate {
        func didTapChoice(name: String)
    }
    
  • 在第四个选项卡视图控制器中添加代理变量:

    var selectionDelegate: SelectionDelegate!
    
    selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
    
    import UIKit
    
    class FirstTabViewController: UIViewController {
    
        @IBOutlet weak var displayLabel: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    extension FirstTabViewController: SelectionDelegate {
        func didTapChoice(name: String) {
            displayLabel.text = name
        }
    }
    
    import UIKit
    
    protocol SelectionDelegate {
        func didTapChoice(name: String)
    }
    
    class SecondTabViewController: UIViewController {
        var selectionDelegate: SelectionDelegate!
    
        @IBOutlet weak var textField: UITextField!
    
        @IBAction func goToFirstTabButtonTapped(_ sender: UIButton) {
            selectionDelegate.didTapChoice(name: textField.text!)
            tabBarController?.selectedIndex = 0
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
        }
    }
    
  • 在@iAction中调用委托变量的函数:

    selectionDelegate.didTapChoice(name: textField.text!)
    tabBarController?.selectedIndex = 0
    
  • 将选择代理指定给第二个viewcontroller的viewDidLoad()中的第一个viewcontroller:

    var selectionDelegate: SelectionDelegate!
    
    selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
    
    import UIKit
    
    class FirstTabViewController: UIViewController {
    
        @IBOutlet weak var displayLabel: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    extension FirstTabViewController: SelectionDelegate {
        func didTapChoice(name: String) {
            displayLabel.text = name
        }
    }
    
    import UIKit
    
    protocol SelectionDelegate {
        func didTapChoice(name: String)
    }
    
    class SecondTabViewController: UIViewController {
        var selectionDelegate: SelectionDelegate!
    
        @IBOutlet weak var textField: UITextField!
    
        @IBAction func goToFirstTabButtonTapped(_ sender: UIButton) {
            selectionDelegate.didTapChoice(name: textField.text!)
            tabBarController?.selectedIndex = 0
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
        }
    }
    
  • 在实现协议功能的第一个选项卡视图控制器底部添加扩展:

    extension FirstTabViewController: SelectionDelegate {
        func didTapChoice(name: String) {
            displayLabel.text = name
        }
    }
    
  • 以下是第一个viewcontroller的结果:

    var selectionDelegate: SelectionDelegate!
    
    selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
    
    import UIKit
    
    class FirstTabViewController: UIViewController {
    
        @IBOutlet weak var displayLabel: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    extension FirstTabViewController: SelectionDelegate {
        func didTapChoice(name: String) {
            displayLabel.text = name
        }
    }
    
    import UIKit
    
    protocol SelectionDelegate {
        func didTapChoice(name: String)
    }
    
    class SecondTabViewController: UIViewController {
        var selectionDelegate: SelectionDelegate!
    
        @IBOutlet weak var textField: UITextField!
    
        @IBAction func goToFirstTabButtonTapped(_ sender: UIButton) {
            selectionDelegate.didTapChoice(name: textField.text!)
            tabBarController?.selectedIndex = 0
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
        }
    }
    
    以下是第二个viewcontroller的结果:

    var selectionDelegate: SelectionDelegate!
    
    selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
    
    import UIKit
    
    class FirstTabViewController: UIViewController {
    
        @IBOutlet weak var displayLabel: UILabel!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    extension FirstTabViewController: SelectionDelegate {
        func didTapChoice(name: String) {
            displayLabel.text = name
        }
    }
    
    import UIKit
    
    protocol SelectionDelegate {
        func didTapChoice(name: String)
    }
    
    class SecondTabViewController: UIViewController {
        var selectionDelegate: SelectionDelegate!
    
        @IBOutlet weak var textField: UITextField!
    
        @IBAction func goToFirstTabButtonTapped(_ sender: UIButton) {
            selectionDelegate.didTapChoice(name: textField.text!)
            tabBarController?.selectedIndex = 0
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            selectionDelegate = tabBarController?.viewControllers?[0] as? SelectionDelegate
        }
    }
    

    在第一个选项卡的视图控制器中编写一个方法(例如yourCustomMethod()),然后您可以使用以下方法从第四个选项卡的视图控制器访问该方法:-

        if let navigationController = self.tabBarController?.childViewControllers[0],
            let firstVC = navigationController.childViewControllers[0] as? FlightsHomeViewController {
    
            let yourData = ["username": "something"]
            firstVC.yourCustomMethod(data: yourData)
        }
    
  • 第一个VC中。收听登录通知
  • 登录VC中。当用户登录时。发布登录通知
    每个选项卡中的第一个控制器是否为导航控制器?还是视图控制器?@VincentJoy导航控制器这可能会起作用,但并不被认为是理想的解决方案:为什么不在那里创建一个变量,然后在切换到第一个选项卡时检索该变量?