简体   繁体   中英

How to segue from UINavigationController (storyboard) to UIViewController programmatically

I'm completely stumped on this problem.

I have an app whose view controllers are mostly made on the storyboard. However, there are a few that are made entirely with code (programmatically).

The entry point for my app is a TabBarController that feeds in to many different Navigation Controllers, each with its own UIViewController.

Here is a visual representation: 在此输入图像描述

I have no problem navigating to any of the proper view controllers (who are represented in the storyboard) using the Tab Bar Items. However, when I choose a Tab Bar Item that does not have a direct segue on the storyboard - because its UIViewController is built entirely through code - I cannot segue to the proper class.

I've added a *.swift class to the NavigationController that has a ViewDidLoad() function. This function is NEVER called when I choose the appropriate Tab Bar Item. The strange part is that there is a single Navigation Controller that is connected to a UIViewController (displayed in the storyboard) and has a *.swift file attached to it. Its ViewDidLoad() does get called when the proper Tab Bar Item is selected. I've tried to attach multiple *.swift files (all inheriting from UINavigationController) but I cannot get a single one to call the appropriate ViewDidLoad() function.

Edit: As you can see in the image, the bottom left Navigation Controller does not have a segue extending from it. However, there is a class attached to it and inside its ViewDidLoad() function is where the segue takes place

class WeaponsNavController: UINavigationController
{

    override func viewDidLoad()
    {          
         self.navigationController?.pushViewController(WeaponsTableVC(), animated: true)
    }

}

As you can see, I'm trying to connect to the WeaponsTableVC() which is a view controller created entirely through code. However, this class does not seem to get called at all despite being directly connected to the Navigation Controller in the storyboard.

If the view controller does not have an associated nib file, this method creates a plain UIView object instead. If you do not provide a nib file when instantiating the view controller, UIViewController calls it's loadView() method which creates a plain UIView object and assigns it to its view property.

// Assuming TestViewController is the view to load
let viewController = TestViewController()
navigationController?.pushViewController(viewController, animated: true)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM