0

I'm trying to create a UINavigationController and two UIVewControllers programmatically and be able to transition from the first VC to the second VC and back using the navigation controller. I've tried different methods for getting this to work. I've left some lines in the source code commented out to give hints of things that I've tried.

With the current method I'm getting this warning (it doesn't work):

Warning: Attempt to present <x.VC2: 0x7fd27240df00> on <x.VC1: 0x7fd27240db60> whose view is not in the window hierarchy!

In AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let navcon = UINavigationController()
    //navcon.viewControllers = [VC1(), VC2()]    // tried this without success
    navcon.viewControllers = [VC1()]

    window = UIWindow()
    window?.makeKeyAndVisible()
    //window?.rootViewController = VC1()         // one variation that I tried

    window?.rootViewController = navcon.viewControllers[0]
    return true
}

In VC1.swift:

var navcon: UINavigationController!
let vc2 = VC2()

//---------------------------------------------

override func viewDidLoad() {
    super.viewDidLoad()
    navcon = self.navigationController
    //navcon?.viewControllers.append(vc2)
    let x = navcon?.viewControllers
    let n = x?.count
}

//---------------------------------------------

func triggeredFunction()  {
    self.present(vc2, animated: true, completion: nil)
    //navcon.pushViewController(vc2, animated: true)
}
0

1 Answer 1

1

Result:enter image description here

Code:

AppDelegate:

    window = UIWindow()
    let firstVC = FirstViewController()
    let navigationController = UINavigationController(rootViewController: firstVC)
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

FirstVC:

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .green

    let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
    button.backgroundColor = .red
    view.addSubview(button)
    button.addTarget(self, action: #selector(showSecondVC), for: .touchUpInside)
}

@objc func showSecondVC() {
    let secondVC = SecondViewController()
    navigationController?.pushViewController(secondVC, animated: true)
}

SecondVC:

view.backgroundColor = .yellow

Hope it helps! Please let me know if it works for you or not.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it works. Thank you very much. But would it be better to instantiate the SecondViewController only once in viewDidLoad()?
Yes definitely. You can also make it lazy var so that it's instantiated only when user taps it I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.