-1

I have a viewController (HomeViewController()) which is embedded into a NavigationCotroller and I have a horizontal CollectionView with 6 cells at the bottom of my viewController and I have implemented the delegate method didSelectItemAt indexPath: IndexPath

Now when I select the any of the cell it will navigate to the respective viewController(self.navigationController?.pushViewController(ViewControllerInstance, animated: true)) lets call it "NewVC1()" as I am using pushViewController, it will create a back button for me and if I click on the back button it will pop back to the previous viewController. It is general scenario and working as expected.

In my case after selecting the collectionViewCell in "HomeViewController()" I will go the "NewVC1()" and in there I have the same CollectionView as I have in the previous viewController(HomeViewController()) and now if I select any of the cells in the present viewController(NewVC1()) again I will navigate to the respective viewController(NewVC2()) same like before using pushViewController and Now, if I click on the back button in the "NewVC2()", I don't want to go to the "NewVC1()" and instead of that I want to go to "HomeViewController()".. How can I achieve this, is there any possibility of doing this?

I hope you understand what I am trying to convey and I am sorry if you can't understand my bad english and also sorry as I think this question looks very confusing..

EDIT:

Symbolically:

let VC1 = HomeViewController() let VC2 = NewViewController1() let VC3 = NewViewController2()

Navigation: VC1 -> VC2 -> VC3 and if want to come back to VC1,

general case:

VC3 -> VC2 and VC2 -> VC1 (possible)

my case:

VC3 -> VC1 (?)

2 Answers2

1

Instead of pushing from VC2 to VC3 use setViewControllers. Then when you press back button from VC3 it will come back to VC1

Use this code in VC2 collectionView didSelectItemAt

let vc3 = self.storyboard?.instantiateViewController(withIdentifier: "VC3") as? VC3
if var viewControllers = self.navigationController?.viewControllers {
    viewControllers.removeLast()
    viewControllers.append(vc3)
    self.navigationController?.setViewControllers(viewControllers, animated: true)
}

enter image description here

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • Thanks for the reply bro but I am pushing through collectionView. In the HomeViewController I have collectionView of 6 items and each item navigates to a different viewController and every individual viewController has the same collectionView like in HomeViewController where again I can navigate to any of the ViewControllers but when I press "Back" button it should come to HomeViewController. I am newly learning programming, I don't know anything about setViewControllers and I don't know where to use the code that you suggested. – Venkatesh Chejarla May 31 '18 at 02:58
  • I understood your question. You can use this answer for your concern. – RajeshKumar R May 31 '18 at 03:08
  • But, where should I use the code you suggested? and what is the newVC? – Venkatesh Chejarla May 31 '18 at 04:54
0

You can achieve this in 2 ways :

First way - Create your own back button :

let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = backButton  

func back(sender: UIBarButtonItem) {
    self.navigationController?.popToRootViewController(animated: true)
}  

Second way - Use isMovingFromParentViewController :

override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)

if self.isMovingFromParentViewController {
    self.navigationController?.popToRootViewController(animated: true)
}

}
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • Thanks for the quick response mate and I tried both the ways and still can't get what I want. It is working normally as before. I edited the question for clarity, please take a look on edited part.. – Venkatesh Chejarla May 30 '18 at 12:52