0

I have a question about Sending the value back to the main page After login-> Page 1-> page2-> press home button -> page 1 When I go, there is no problem with the delivery. But I want to press the home button and return the value to page1

this out put on page 2

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

See from the picture

enter image description here

After login -> page 1 -> go to page 2 this code on page 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!
        }
    }

press Home button on page 2 -> go to page 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!)
                }
            }
        }
    }

3 Answers3

1

Seems you can create method in Home screen with data you want to pass as a parameter and call it from Page 2.

In Home screen

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

From Page 2

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

Also, you can pass data by creating property in home screen and passing data from Page2.

Ashwinkumar Mangrulkar
  • 2,956
  • 3
  • 15
  • 17
0

Instead of using segue you can use navigationController controller and it will get the same instance of page 1 (VC) that is already loaded into navigation controller. If you use the segue it will create new instance that will present on the top of page 2 (VC). So instead of using new instance use the same instance that is created already.

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

}

Qazi Ammar
  • 953
  • 1
  • 8
  • 23
0

You could try post notification which send your data once you click the home button,

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

In the home controller add this in 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/

kannan
  • 354
  • 1
  • 7