问题
I have a UIViewController
which was created in a storyboard and has been assigned a storyboard id. This UIViewController
is instantiated in various places in code and therefor it didn't make sense to create segues from all the different locations.
The view controller needs to be contained in a UINavigationController in order to function properly because it uses the various navigation bar items and can push additional view controllers.
The view controller is instantiated from code as follows:
// some where in code that needs to present the custom view controller
UIViewController *viewControllerToPresent = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[self.viewController presentViewController: viewControllerToPresent animated:YES];
problem is that the view controller isn't contained in a navigation controller as expected from the storyboard. I've tried assigning its containing UINavigationController the storyboard id, but that caused problems in existing code in prepareForSegue
which expected an instance of CustomViewController
The obvious option to fix this would be to add creation of a UINavigationController before presenting the CustomViewController
:
UIViewController *viewControllerToPresent = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: viewControllerToPresent];
[self.viewController presentViewController: nav animated:YES];
But this doesn't fix the problem either because the navigation items from the storyboard aren't added correctly.
What to me seems more reasonable, is to have the CustomViewController
inherit from UINavigationController
and build the whole thing in storyboard such that the custom view controller contains its own navigation bar.
I've seen classes such as UIImagePickerController
which inherit from UINavigationController
and I believe it is exactly this case they try to solve.
Problem is that when I change the CustomViewController to inherit from UINavigationController
, when the view controller is instantiated from the storyboard id , the view returns empty. The storyboard isn't building the view at all.
Is this something that can be fixed?
回答1:
You should create the UINavigationController in your storyboard and connect it to your UIViewController.
Then, instantiate and present the UINavigationController:
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:@"MyNavigationController"];
[self.viewController presentViewController: nav animated:YES];
Your UIViewController will be loaded automatically since it is connected in the StoryBoard. All the buttons and configuration you predefine in the Storyboard will load.
回答2:
You haven't described your situation very clearly.
If you want to push view controllers onto a navigation stack, you have to have a navigation controller on-screen.
You can set up a storyboard where the root view controller is a custom subclass of UINavigationController. That works fine. Then you can instantiate new view controllers using the code you posted and push them onto the navigation stack of your navigation controller. The method for pushing a view controller onto the navigation stack is
pushViewController:animated:
However, the call you are using, presentViewController:animated:, is a deprecated method to present a modal view controller, not push a view controller onto a navigation controller stack.
(The new version of that method is to present a modal view controller is presentViewController:animated:completion:
)
Your second block of code is no better:
UIViewController *viewControllerToPresent = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: viewControllerToPresent];
[self.viewController presentViewController: nav animated:YES];
That code creates a navigation controller, then ignores it and still attempts to present viewControllerToPresent as a modal on top of self.viewController, whatever that is.
The method to push a view controller onto a navigation controller's stack is pushViewController:animated:. You send that message to the navigation controller, but the navigation controller has to be on-screen in order for it to do anything.
Here is what I suggest you do:
Select your storyboard's initial view controller. Pick "embed in>navigation controller" from the editor menu. This will create a new, blank navigation controller and make it the initial view controller for your storyboard.
来源:https://stackoverflow.com/questions/26654583/creating-a-custom-uinavigationcontroller-in-storyboard-isnt-instantiating-the-v