Displaying a uiviewcontroller embeded in a navigation controller with button tapped

King

New Member
#1
I am trying to present a viewcontroller embedded in a navigation caontroller by tapping a button. Everytime I tap the button the app crashes saying Could not cast value of type 'UINavigationController' (0x11320f420) to
my code to display the Viewcontroller
Mã:
@IBAction func skipButtonTapped(_ : UIButton) {

        let viewController:
            CategoryVC = UIStoryboard(name: "CategorySB", bundle: nil).instantiateViewController(withIdentifier: Constants.CATEGORY_VC) as! CategoryVC


        self.present(viewController, animated: true, completion: nil)
    }
any help thanks
 

Admin

Administrator
Thành viên BQT
#2
You can try
Mã:
let nav = UIStoryboard(name: "CategorySB", bundle: nil).instantiateViewController(withIdentifier: Constants.CATEGORY_VC) as! UINavigationController
self.present(nav, animated: true, completion: nil)
//

if you wan to access root
Mã:
if let cat = nav.topViewController as? CategoryVC {

}
 
Top