Rashed
Rashed

Reputation: 2425

Push view controller from a Child View controller

From a button action, I have presented a viewController, from that presented VC, add another button and on that button action added a childView like this -

let vc = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "APPopUpViewControllerViewController") as! APPopUpViewControllerViewController
self.addChild(vc)
vc.view.frame = self.view.frame
self.view.addSubview(vc.view)
vc.didMove(toParent: self)

All are working perfectly till now, but when i try to push a viewController from this child VC , it does not work. How can i push a viewController from a childView ??

Push viewController code -

let storyboard = UIStoryboard(name: "WPLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WPSigninViewController") as! WPSigninViewController
self.navigationController?.pushViewController(vc, animated: true)

Upvotes: 0

Views: 860

Answers (3)

Debashish Das
Debashish Das

Reputation: 919

How do you present your ViewController ? If You have pushed it to a navigation controller, then your code is perfect and it should work I think.

If not, then try to present the WPSigninViewController modally as

let storyboard = UIStoryboard(name: "WPLogin", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WPSigninViewController") as! WPSigninViewController
self.present(vc, animated: true)

and It should work.

If ViewController is your rootController, you can add a NavigationController to it :

let viewController = ViewController(nibName: nil, bundle: nil)
let navigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100541

You can try

print(parent)
print(parent?.navigationController)   
self.parent?.navigationController?.pushViewController(vc, animated: true)

Upvotes: 1

Duncan C
Duncan C

Reputation: 131491

My guess is that self.navigationController is nil, so your optional chaining is failing.

Upvotes: 1

Related Questions