How to show one of two ViewControllers by clicking the "Cancel" button?

I have one ViewController that I can click on by clicking TabBarItem or from Table View Controller B by clicking the plus in the navigation bar.

My problem

How, by clicking on the "Cancel" button on the ViewController, I can go to Table View Controller B if I got into the ViewController from the table view controller B, by clicking the plus or go to the "Table Controller" if I got into the ViewController by selecting the second one ( blue) tab item?

I want to set two actions on the Cancel button on the ViewController - depending on the previous controller, I want to go to TableViewControllerA or TableViewControllerB, is this possible?

More details

The first version of the transition: By clicking the Plus button on TableViewControllerB, I go to ViewController and in ViewController, I click on the "Cancel" button and return to TableViewControllerB.

Second version of the transition: By clicking on the second TabBarItem on the TabBar controller, I go to the ViewController and the ViewController, I click the Cancel button and return to TableViewControllerA.

Outline of my project

+4
source share
1 answer

There are two ways to achieve what you want, according to your controller’s flow chart.

  • Pop server to the root controller.
  • Pop to Specific view controller (TableViewController A)

, .

/ :

@IBAction btnCancel_Action(button: UIButton) {

    // 1. Pop to root view controller
    self.navigationController?.popToRootViewController(animated: true)

    // OR
    // 2. Pop to Specific view controller (TableViewController A)

   if let navController = self.navigationController {

        for viewcontroller in navController.viewControllers {
            // `TableViewControllerA` class name for view controller or you can use instance of `TableViewControllerA` also with `viewcontroller == <TableViewControllerA>`

            if viewcontroller is <TableViewControllerA> { 
            //if viewcontroller == <IntanceOfTableViewControllerA> {  
               self.navigationController?.popToViewController(viewcontroller, animated: true)
                break
            }

        }
    }
}



:

@IBAction btnCancel_Action(button: UIButton) {
 if let tabController = self.tabBarController {
            if tabController.selectedIndex == 0 {
                self.navigationController?.popViewController(animated: true)
            } else if tabController.selectedIndex == 1 {
                self.tabBarController?.selectedIndex = 0


                // If your tabbar 0 has TableViewControllerB is active on screen then use following code
                if let navController = self.tabBarController?.navigationController {
                    navController.popToRootViewController(animated: false)

                    /*
                    //or
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
                        navController.popToRootViewController(animated: false)
                    })
                     */
                }


            }
        }
}
+1

Source: https://habr.com/ru/post/1685154/


All Articles