gpt4 book ai didi

swift - 如何在 Swift 中管理 ViewController 之间的状态?

转载 作者:行者123 更新时间:2023-11-30 10:40:28 25 4
gpt4 key购买 nike

我正在努力解决我认为是 ViewController 之间的状态问题。

导航路径如下;

1.用户从VenueDetailsVC导航至DiscoverVC并通过 Venue这样的对象:

func presentDiscoverVC(){

guard let discoverVC = storyboard?.instantiateViewController(withIdentifier: "DiscoverVC") as? DiscoverVC else { return }

discoverVC.venue = self.venue!
discoverVC.showBackButton = true

DispatchQueue.main.async {
self.presentDetail(discoverVC)
}

}

2.用户可以从 DiscoverVC 导航至PreferencesVC并通过相同的 Venue清除本地对象时 Venue这样的对象:

class DiscoverVC: UIViewController{

var venue: Venue?{
didSet{
print("DiscoverVC venue name: \(venue?.name ?? "venue name")")
}
}

@IBAction func preferencesBtnTapped(_ sender: Any) {

guard let preferencesVC = storyboard?.instantiateViewController(withIdentifier: "PreferencesVC") as? PreferencesVC else { return }

preferencesVC.selectedVenue = self.venue!

presentDetail(preferencesVC)

}

override func viewWillAppear(_ animated: Bool) {

print("inside viewWillAppear venue: \(venue?.name ?? "venue name")")

}


override func viewWillDisappear(_ animated: Bool) {

venue = nil

}
}

3.User 然后导航回 DiscoverVC来自PreferencesVC通过同样的Venue对象

@IBAction func doneBtnTapped(_ sender: Any) {

guard let discoverVC = storyboard?.instantiateViewController(withIdentifier: "DiscoverVC") as? DiscoverVC else { return }

discoverVC.venue = self.selectedVenue!

DataService.run.updateUserDiscoveryPreferences(forUID: Auth.auth().currentUser!.uid, discoverableBool: discoverable!, preferences: preferences) { (success) in

self.dismissDetail()
}

}

问题:是Venue吗? DiscoverVC 内的对象为零,即使它已从 PreferencesVC 传回在上面的第三步??

这是控制台输出:

enter image description here

最佳答案

当你这样做时:

@IBAction func doneBtnTapped(_ sender: Any) {

guard let discoverVC = storyboard?.instantiateViewController(withIdentifier: "DiscoverVC") as? DiscoverVC else { return }

discoverVC.venue = self.selectedVenue!

您没有设置呈现首选项 VC 的发现 VC 的地点。您正在设置全新发现 VC 的 field

您可以使用委托(delegate)模式将venue传回。

创建一个PreferencesVCDelegate:

protocol PreferencesVCDelegate: class {
func preferencesVC(_ vc: PreferencesVC, didDismissWithVenue venue: Venue)
}

PreferencesVC 中添加一个 delegate 属性:

weak var delegate: PreferencesVCDelegate?

将设置 venue 的行替换为对委托(delegate)方法的调用:

@IBAction func doneBtnTapped(_ sender: Any) {

guard let discoverVC = storyboard?.instantiateViewController(withIdentifier: "DiscoverVC") as? DiscoverVC else { return }

delegate?.preferencesVC(self, didDismissWithVenue: self.venue)

当您呈现 PreferencesVC 时,将 self 设置为委托(delegate):

@IBAction func preferencesBtnTapped(_ sender: Any) {

guard let preferencesVC = storyboard?.instantiateViewController(withIdentifier: "PreferencesVC") as? PreferencesVC else { return }

preferencesVC.selectedVenue = self.venue!
preferencesVC.delegate = self

presentDetail(preferencesVC)

}

并使DiscoverVC符合PreferencesVCDelegate。这是您设置发现 VC 的 venue

的地方
extension DiscoverVC: PreferencesVCDelegate {
func preferencesVC(_ vc: PreferencesVC, didDismissWithVenue venue: Venue) {
self.venue = venue
}
}

关于swift - 如何在 Swift 中管理 ViewController 之间的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56919023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com