how to add custom actions to UIBarButtonItem

130 views Asked by At

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

There are 2 answers

3
RajeshKumar R On BEST ANSWER

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

1
Nitish On

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

}