Ios 切换选项卡时向控制器发送数据
我的应用程序有4个标签。在第四个选项卡上,有一个“登录”应用程序的选项,它将用户带到登录控制器。当用户登录时,我想将用户切换回第一个选项卡,我使用以下代码执行此操作:Ios 切换选项卡时向控制器发送数据,ios,swift,xcode,swift4,Ios,Swift,Xcode,Swift4,我的应用程序有4个标签。在第四个选项卡上,有一个“登录”应用程序的选项,它将用户带到登录控制器。当用户登录时,我想将用户切换回第一个选项卡,我使用以下代码执行此操作: self.tabBarController?.selectedIndex = 0 但是,我想在用户登录后刷新第一个选项卡上的内容(因为内容是特定于用户的)。这意味着,当用户切换选项卡时,我需要传递一些数据,以便让第一个选项卡中的控制器知道如何刷新数据 在选项卡之间切换以刷新内容时,如何将数据发送到第一个选项卡?还是有更好的解决方
self.tabBarController?.selectedIndex = 0
但是,我想在用户登录后刷新第一个选项卡上的内容(因为内容是特定于用户的)。这意味着,当用户切换选项卡时,我需要传递一些数据,以便让第一个选项卡中的控制器知道如何刷新数据
在选项卡之间切换以刷新内容时,如何将数据发送到第一个选项卡?还是有更好的解决方法?
您可以通过使用通知/观察者或委托模式来完成
因为它似乎是一对一关系,所以您可能应该使用委托模式,因为通知和观察者用于一对多关系
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
}
}
selectionDelegate.didTapChoice(name: textField.text!)
tabBarController?.selectedIndex = 0
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
}
}
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)
}
每个选项卡中的第一个控制器是否为导航控制器?还是视图控制器?@VincentJoy导航控制器这可能会起作用,但并不被认为是理想的解决方案:为什么不在那里创建一个变量,然后在切换到第一个选项卡时检索该变量?