0

我有一个关于将值发送回主页的问题登录后->第1页->第2页->按主页按钮->第1页当我去的时候,交付没有问题。但我想按下主页按钮并将值返回到 page1

这在第 2 页上

pShipmentDCBeforeLoad==>Optional("4505023274")
pPlanDateDCBeforeLoad==>Optional("20190119")
TruckIdDCBeforeLoad==>Optional("2")
ptruckNoDCBeforeLoad==>Optional("60-7624")
pShipmentDCBeforeLoad==>Optional("4505023274")

从图片上看

在此处输入图像描述

登录后->第1页->转到第2页此代码在第2页

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//      Check segue identifier
        if segue.identifier == "gotoAfterLoadingDC" {
//          Get SecondVC
            let destinationVC = segue.destination as! AfterLoadViewController
//          Pass text to SecondVC
            destinationVC.pShipmentDCAfterLoad = pShipmentDCBeforeLoad!
            destinationVC.pPlanDateDCAfterLoad = pPlanDateDCBeforeLoad!
            destinationVC.TruckIdDCAfterLoad = TruckIdDCBeforeLoad!
            destinationVC.ptruckNoDCAfterLoad = ptruckNoDCBeforeLoad
            destinationVC.PlanDetailIdDCAfterLoad = PlanDetailIdDCBeforeLoad!
        }
    }

按第 2 页上的主页按钮 -> 转到第 1 页

@IBAction func BacktoPlandata(_ sender: UIBarButtonItem) {
        DispatchQueue.main.async {
            //self.performSegue(withIdentifier: "BeforeloadBacktoplandata", sender: "self")
            func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                //      Back to home
                if segue.identifier == "BeforeloadBacktoplandata" {
                    //Get SecondVC
                    let destinationVC = segue.destination as! PlanDataViewController
                    //Pass text to SecondVC
                    destinationVC.getpShipmentPickerView.text! = self.pShipmentDCBeforeLoad!
                    destinationVC.getpPlanDatePickerView.text! = self.pPlanDateDCBeforeLoad!
                    destinationVC.ptruckidDCPlanData = self.TruckIdDCBeforeLoad!
                    destinationVC.ptruckNoDCPlanData = self.ptruckNoDCBeforeLoad
                    destinationVC.pPlandDetailIdDCPlanData = self.PlanDetailIdDCBeforeLoad!

//                  Show log back to plandata
                    print("pShipmentDCBeforeLoad==>\(String(describing: self.pShipmentDCBeforeLoad))")
                    print("pPlanDateDCBeforeLoad==>\(String(describing: self.pPlanDateDCBeforeLoad))")
                    print("TruckIdDCBeforeLoad==>\(String(describing: self.TruckIdDCBeforeLoad))")
                    print("ptruckNoDCBeforeLoad==>\(String(describing: self.ptruckNoDCBeforeLoad))")
                    print("pShipmentDCBeforeLoad==>\(String(describing: self.pShipmentDCBeforeLoad))")

//                  call fuction
                    print("//////////////-getPlanDetail-BacktoPlandata-/////////////////")
                    self.getPlanDetailDCbeforeLoadingViewController(pshipment: self.pShipmentDCBeforeLoad!, pplanDate: self.pPlanDateDCBeforeLoad!)
                }
            }
        }
    }
4

3 回答 3

1

似乎您可以在主屏幕中使用要作为参数传递的数据创建方法并从第 2 页调用它。

在主屏幕中

func someData(param1: String, param2: String) { }

从第 2 页开始

let destinationVC = segue.destination as! PlanDataViewController
destinationVC.someData(param1: "ABC", param2: "XYZ")

此外,您可以通过在主屏幕中创建属性并从 Page2 传递数据来传递数据。

于 2019-07-31T06:19:29.153 回答
0

除了使用 segue,您可以使用 navigationController 控制器,它将获得已加载到导航控制器中的第 1 页(VC)的相同实例。如果您使用 segue,它将创建新实例,该实例将显示在第 2 页(VC)的顶部。因此,不要使用新实例,而是使用已经创建的相同实例。

@IBAction func BacktoPlandata(_ sender: UIBarButtonItem) {

let n: Int! = self.navigationController?.viewControllers?.count
let destinationVC = self.navigationController?.viewControllers[n-1] as! UIViewController

destinationVC.getpShipmentPickerView.text! = self.pShipmentDCBeforeLoad!
destinationVC.getpPlanDatePickerView.text! = self.pPlanDateDCBeforeLoad!
destinationVC.ptruckidDCPlanData = self.TruckIdDCBeforeLoad!
destinationVC.ptruckNoDCPlanData = self.ptruckNoDCBeforeLoad
destinationVC.pPlandDetailIdDCPlanData = self.PlanDetailIdDCBeforeLoad!

self.navigationController.popViewController(animated: true)

}

于 2019-07-31T06:25:25.930 回答
0

您可以尝试发布通知,一旦您单击主页按钮就会发送您的数据,

NotificationCenter.default.post(name: Notification.Name(rawValue: "anyname"), object: ["TruckID": self.TruckIdDCBeforeLoad]) 

在 home 控制器中,在 viewdidload 中添加这个,

NotificationCenter.default.addObserver(self, selector: #selector(refresh(_:)), name: NSNotification.Name(rawValue: "anyname"), object: nil)

https://www.hackingwithswift.com/example-code/system/how-to-post-messages-using-notificationcenter

https://learnappmaking.com/notification-center-how-to-swift/

于 2019-07-31T07:23:44.230 回答