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