【问题标题】:I have a question about sending the value back to the main page [IOS] [duplicate]我有一个关于将值发送回主页的问题 [IOS] [重复]
【发布时间】:2019-12-08 12:54:31
【问题描述】:

我有一个关于将值发送回主页的问题 登录后->第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!)
                }
            }
        }
    }

【问题讨论】:

    标签: json swift xcode10.3


    【解决方案1】:

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

    在主屏幕中

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

    从第 2 页开始

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

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

    【讨论】:

      【解决方案2】:

      您可以使用navigationController 控制器而不是使用segue,它将获得已加载到导航控制器中的第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)
      

      }

      【讨论】:

        【解决方案3】:

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

        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/

        【讨论】:

          猜你喜欢
          • 2020-01-27
          • 2021-10-29
          • 2019-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多