-1
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    let vc = storyboard?.instantiateViewController(identifier: 
    "PersonViewController") as? PersonViewController               
    
    vc?.names = persons[indexPath.row].emer!
    vc?.lastnames = persons[indexPath.row].mbiemer!
    vc?.delegate = self
    PersonViewController.indexes = indexPath.row 
    self.navigationController?.pushViewController(vc!, animated: true)

}`

I have a situations like this:

First ViewController is a collectionView, the second is a viewcontroller which is allowed to add new Person when I tap a button and works perfectly. I have used delegates and Core Data for local memory.

Also the second ViewController has another button to edit person. When I press button a new viewController appears with extension UIAdaptivePresentationControllerDelegate. This viewcontroller consists of 2 buttons and 2 textfields. So when I want to press save button I want to go to the first viewcontroller (collectionview list) and when to press cancel to go back to the second viewcontroller.

Viewcontrollers are created with pushViewController method.

image

Please anyone help what should I use?

then in PersonViewController I call this inside button edit.

@objc func editCell(){
    let vc = storyboard?.instantiateViewController(identifier: 
    "ModalPresentationViewController") as? 
     ModalPresentationViewController

     navigationController?.pushViewController(vc!, animated: true)
    
}

Now the code in the las ViewController which is ModalViewController

@objc func savePerson(){
    if editNameTextfield.text == "" || editlastNameTextfield.text == ""{
        self.errorLbl.alpha = 1
    }
    else{
        let vc = ViewController()
        guard let textName = editNameTextfield.text else{
            return
        }
        guard let textLastName = editlastNameTextfield.text else{
            return
        }

        let index = PersonViewController.indexes
        DispatchQueue.main.async {[self] in
            if editDelegate != nil{
                self.editDelegate!.editPerson(editedName: textName, editedLastname: textLastName, index: index)
            }
        }
//            What should I call here??
       
    }
}
  • now it is edited. I just want a guide what to use to solve this – Donald Cela Nov 08 '20 at 14:03
  • Are you handling your navigation via code or are you using Segues in Storyboard? – DonMag Nov 08 '20 at 14:08
  • So what's the problem? `popToViewController` lets you go back to whatever view controller you want. https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller – matt Nov 08 '20 at 14:09
  • No I'm not using segues. In ViewController just embed Navigation Controller and used storyboard identifier to push to another ViewController. Also all views in 3 ViewControllers are created by code not in Main.storyboard – Donald Cela Nov 08 '20 at 14:13
  • Can't save data with popToViewController. I success saving data only with popViewController but this method brings me back to previous screen not in the first one – Donald Cela Nov 08 '20 at 14:29
  • Have you tried `popToRootViewController`? By the way, if you aren't using Segues, that means you should have quite a bit of code you could post instead of a hand-drawn explanation. That would be *very* helpful to actually *duplicate* whatever issue you are facing. Strip things down - it sounds like you have 4 VCs... A, B, C, and the navigation controller. Add *minimal* logic/views to each to duplicate. Post that. –  Nov 08 '20 at 15:20
  • I puted some code! – Donald Cela Nov 08 '20 at 16:46

1 Answers1

0

You can use something like:

func popTo(_ type: AnyClass) {
    guard let controllers = navigationController?.viewControllers else {return}
    
    for controller in controllers {
        if controller.classForCoder == type {
            navigationController?.popToViewController(controller, animated: true)
            break
        }
    }
}

And invoke the function depending on the condition as:

popTo(A.classForCoder())

EDIT: Above solution works only if ViewControllers B and C presented via a navigation controller and are in the same navigation stack.

Since you present the ViewController C modally, below answer should work:

Make these changes to your ViewController B that is presenting C:

class B: UIViewController, PresenterDelegate {
// some functions

    func popToPrevious() {
        navigationController.popViewController(animated: false)
    }

    @objc func editCell(){
        let vc = storyboard?.instantiateViewController(identifier: 
        "ModalPresentationViewController") as? 
         ModalPresentationViewController
        vc.presenterDelegate = self

        present(vc, animated: true, completion: nil)

    }
}

And in your ViewController C:

class C {
    var presenterDelegate: PresenterDelegate? = nil

    //

    @objc func savePerson(){
        //
        //
        dismiss(animated: true, completion: {
            self.presenterDelegate?.popToPrevious()
        })
    }
}

Also add PresenterDelegate protocol to a new file or below ViewControllers B or C

protocol PresenterDelegate {
    func popToPrevious()
}
  • Thanks for your answer, it's a good idea but still can't get to the A ViewController. I have tried a lot of solutions like this. The problem is that I forgot to mentions is that this function I invoke inside a dismiss completion because C- ViewController is a present modal. So every solution brings me back to the B - View Controller. I have tried also with popToRootViewController but still nothing – Donald Cela Nov 09 '20 at 15:26
  • This is exactly what is my problem about https://stackoverflow.com/questions/44669698/how-to-dismiss-and-pop-to-viewcontroller-simultaneously but still doesn't work for me – Donald Cela Nov 09 '20 at 15:35
  • Hey Donald, since you stated "PushVC" between B and C in your diagram, this solution was based on the assumption that A, B, C are in the same navigation stack. I'll edit my answer. – Buğra Cansın Göz Nov 09 '20 at 15:43
  • yeap my mistake B to C -> present not push – Donald Cela Nov 09 '20 at 15:46
  • Made some changes, let me know :) – Buğra Cansın Göz Nov 09 '20 at 16:03
  • Solved! This was genius, Thank you very much! – Donald Cela Nov 09 '20 at 16:13
  • Except when call dismiss the animated is better to be false :p – Donald Cela Nov 09 '20 at 16:43
  • Yeah, I did not pay any attention to animations at all :) I would appreciate if you accept my answer, so the community can benefit from it too. – Buğra Cansın Göz Nov 09 '20 at 16:53
  • Sorry first time posting a question, I did it – Donald Cela Nov 09 '20 at 17:31