Navigate from popUpVC to newVC in ios Swift

50 Views Asked by At

I have a SourceViewController from where I am popping up another popUpViewController to select Dates. On the popupViewController I have a Submit button which I have connected as unwind segue to SourceViewController.

When user clicks on Submit button, I have used a function to call an api to collect data based on dates and on the completion I call the unwind segue.

@IBAction func saveClicked(_ sender: UIButton){
 getData(fromDate: frmDt, toDate: toDt){(data) in
  if data != nil{
   self.performSegue(withIdentifier: "unwindsegue", sender: self)
  }
 }
}

On my SourceViewController I already have a segue which pushes to NewViewController that displays data collected using api.

@IBAction func showNewVC(segue: UIStoryboardSegue){
 self.performSegue(withIdentifier: "showNewVC", sender: self)
}

The problem is after unwind segue, when my SourceViewController performs segue to newViewController, it navigates to newViewController for an instant and returns back to sourceViewController.

I dont understand why its happening this way. I have checked my segue also.

If there is better way to perform this kind of popup date then also would be glad to know.

1

There are 1 best solutions below

0
Thijs van der Heijden On

I would recommend looking at the delegate pattern. To do this follow these steps:

In the date picker view controller add something like this:

protocol datePickerDelegate {
  func datePicked(fromDate: frmDt, toDate: toDt)
}

Then add a variable to the date picker vc of type datePickerDelegate like this:

var delegate: datePickerDelegate?

Now when you want to leave the date picker view controller, ask for the data you want and call the datePickerDelegate function we declared above:

delegate?.datePicked(fromDate: ...., toDate: ....)

To make all of this work, you will need to add the source view controller as the delegate for the date picker view controller. To do this you need to expand the segue method somewhat like this:

@IBAction func showNewVC(segue: UIStoryboardSegue){
  if let vc = segue.destination as? DatePickerViewController (make sure this is the correct class) {
    vc.delegate = self
  }
 self.performSegue(withIdentifier: "showNewVC", sender: self)
}

I'm not sure this will work as I can't code it right now. But I recommend you to look up some tutorials on the delegation pattern.