2
votes

when user click a button at some cell (view) i want to push to another tab's viewcontroller

tabbarController
--- nav controller ---- VC A
--- nav controller ---- VC B
--- nav controller ---- VC C ---- VC D

like from viewcontroller A to viewcontroller D

i tried segue

self.window?.visibleViewController?.performSegue(withIdentifier:"homeSegueDetailStatement", sender: self)

worked but without nav bar....

i tried create a nav controller and present , crashed...

if let visibleViewCtrl = UIApplication.shared.keyWindow?.visibleViewController {

            let nav = UINavigationController(rootViewController: visibleViewCtrl)
            let b = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PendingStatementVC") as! PendingStatementViewController
        nav.pushViewController(b, animated:false)
            visibleViewCtrl.present(nav, animated:true, completion:nil)
        }

please help me

EDIT---

actually i want to change tab and nav to second view controller. and i call this func from collectionCell ,that's why i have to ge current viewcontroller

EDIT--- i found what i want

DispatchQueue.global().async {
        DispatchQueue.main.async {
            self.tabBarController?.selectedIndex = 2
                if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DetailStatementVC") as? DetailStatementViewController {

                if let navigator = self.tabBarController?.viewControllers?[2] as? UINavigationController {
                    navigator.pushViewController(controller, animated: true)
                }
            }
        }
    }
5
if you push the viewcontroller then why you again presentingviewcontrollerBhupat Bheda
you need to push from appdelegate or Viewcontroller ?KKRocks
can you show ur storyboard sceneAnbu.Karthik

5 Answers

4
votes

Using the code below, you go from VC-A to VC-D and then you can go back from VC-D to VC-C

let dViewController = self.storyboard?.instantiateViewController(withIdentifier: "DViewController") as! DViewController
let thirdTabNavController = self.tabBarController?.viewControllers?[2] as! UINavigationController
thirdTabNavController.pushViewController(dViewController, animated: false)
self.tabBarController?.selectedIndex = 2
0
votes

Try something like this:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "mainView") as! MainViewController
self.present(nextViewController, animated:true, completion:nil)
0
votes

Try like this

let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let MainViewControllerObj : MainViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
let navigationControllerObj:UINavigationController = UINavigationController.init(rootViewController: MainViewControllerObj)
self.present(navigationControllerObj, animated:true, completion:nil)
0
votes

Kamil3's answer is the best for sure, but if someone need to hide title next to "back" button in navigation bar, I found not very elegant solution, but want to share, because it took me a lot of time:

guard let dViewController = 
    self.storyboard?.instantiateViewController(withIdentifier: "DViewController") as! DViewController,
    let thirdTabNavController = self.tabBarController?.viewControllers?[2] as! UINavigationController else {
  return
}
self.tabBarController?.selectedIndex = 2
// here what you need
thirdTabNavController.viewControllers.first?.title = " "
thirdTabNavController.pushViewController(dViewController, animated: false)
-1
votes
Try below code:

let mainStoryBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = mainStoryBoard.instantiateViewController(withIdentifier: "DcontrollerIdentifier") as! DViewController
 self.navigationController?.pushViewController(nextViewController, animated: true)