将Object传递给另一个ViewController之后,该对象变为nil,Swift?

时间:2019-07-01 06:20:35

标签: swift

我通过声明类的实例并将Venue变量设置为要传递的对象,来将PreferencesVC对象从DiscoverVC传递到venue

在代码执行beginLoadView()时,Venue对象为nil,因此if语句执行不正确

控制台输出:

enter image description here

Class PreferencesVC: UIViewController{

    @IBAction func doneBtnTapped(_ sender: Any) {

        print("inside doneBtnTapped selectedVenue.name: \(selectedVenue?.name ?? "selectedVenue is nil")")

        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()
        }

    }//end func
}



    Class DiscoverVC: UIViewController{

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

override func viewWillDisappear(_ animated: Bool) {
        print("inside viewWillDisappear")

        venue = nil

    }//end func

         override func viewWillAppear(_ animated: Bool) {

                print("inside viewWillAppear")
                beginLoadView()
    }
         func beginLoadView(){

                print("inside beginLoadView venue.name: \(venue?.name ?? "'venue' is nil")")

                if venue != nil {

                    print("Venue var is not empty venue.name: \(String(describing: venue?.name))")

                    Utilities.run.showSVHUDWithStatus(uiView: self.view, status: "Discovering")
                    setupViews()
                    getCurrentLoggedUserProfile()
                    showHudAndStartUpdatingLocation()
                    observeUsersChanges()

                } else {

                    print("Venue var is empty venue.name: \(String(describing: venue?.name))")

                    Utilities.run.showSVHUDWithStatus(uiView: self.view, status: "Discovering")
                    fetchNearestVenue()
                    getCurrentLoggedUserProfile()

                }// end if-else

            }//end func
         }
        }

    Class VenueDetailsVC: UIViewController{

        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)
            }

        }//end func

    }

1 个答案:

答案 0 :(得分:0)

如我所见,discoverVC变量不用于呈现,而您设置地点的discoverVC变量是不同的DiscoverVC对象。您可以在viewWillAppear中尝试discoverVC的打印地址。