4

I`ve one ViewController to which I can hit by clicking on TabBarItem or from Table View Controller B by pressing plus on navigation bar.

My problem

How, by clicking on the Cancel button on the ViewController, I can go to Table View Controller B if I hit to ViewController from Table View Controller B by clicking plus or go to Table View Controller A if I hit to ViewController by selecting the second (blue) tab bar item?

I want set two action to Cancel button on the ViewController - depending on the previous Controller I want to go to TableViewControllerA or TableViewControllerB, is it possible?

Details

First version of the transition: By clicking Plus button on the TableViewControllerB I go to ViewController and on ViewController I click Cancel button and return to TableViewControllerB.

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

Schema of my project

  • Please provide more details your concern in detail by updating your question. So I can help you better. What exactly you want? – Krunal Sep 06 '17 at 13:26
  • This is confusing navigation. User is viewing `Table-A` - and `RedTab` is currently selected... user taps `+` and you want to navigate to `ViewController` and automatically select `BlueTab`? And then have a Cancel button that returns to `Table-A` and selects `RedTab`? And the same (confusing) navigation with `Table-B`??? – DonMag Sep 06 '17 at 13:48
  • try with an updated answer... – Krunal Sep 06 '17 at 13:49

1 Answers1

1

There are two ways to achieve, what you want, according to your view controller flow diagram.

  1. Pop to root view controller
  2. Pop to Specific view controller (TableViewController A)

You need to decide which options should you use, according to your requirement.

Add/replace following code to your cancel button and try both options one after another:

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

        }
    }
}



Edited answer according to question edit: Try this

@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)
                    })
                     */
                }


            }
        }
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Yes, both thins are possible according to flow you have shown in your view controller diagram. Just try second option and set conditions according to your requirement. – Krunal Sep 06 '17 at 13:27
  • Share me more details about two conditions you want to set for cancel button.So I can help you better. – Krunal Sep 06 '17 at 13:28
  • I`m added more details about variants of segues. – Liubov Fedorchuk Sep 06 '17 at 13:37