Tabs do not show when using PushViewController

懵懂的女人 提交于 2019-12-11 12:39:04

问题


I'm trying to use the following code to display a tabbarcontroller

UITabBarController *tc = [[self storyboard] instantiateViewControllerWithIdentifier:@"tabbarcontroller"];
[self.navigationController pushViewController:tc animated:YES];

It does load the view, and I can tell it which of the tabs I want it to default to. The problem is the tabs don't show. From what I've read I gather it has something to do with putting the tab controller inside of the navigation controller, but I couldn't find any solutions.


回答1:


If you use Storyboard, use pushViewController method is a bad choice (also if it work). You have to insert a "segue". Go in the storyboard and while press ctrl button, click on the main controller (which must open the tabViewController) and then release the click on the tabBarController.

Now you have the segue. Click on the circle which appears and choose an identifier for this segue, for example: MainToTab .

Now in your method, you have just to call:

[self performSegueWithIdentifier:@"MainToTab" sender:self];

Moreover, if you want manage the property on the destination controller (by segue), you can implement this method:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"MainToTab"]) {
        UITabViewController *tb = (UITabViewController *)segue.destinationViewController;
        //set the properties...
    }

}

This method is called automatically when you launch the previous method.




回答2:


What you want inside your UITabBarController are UIViewControllers, possibly inside a UINavigationController.

You shouldn't as well push a UITabBarController. You can see Apple's explanation here:

An app that uses a tab bar controller can also use navigation controllers in one or more tabs. When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers.

The most common way to use a tab bar controller is to embed its view in your app’s main window. (...)

Still you can present it modally:

It is possible (although uncommon) to present a tab bar controller modally in your app. Tab bar interfaces are normally installed in your app’s main window and updated only as needed. However, you could present a tab bar controller modally if the design of your interface seems to warrant it. For example, to toggle from your app’s primary operational mode to a completely different mode that uses a tab bar interface, you could present the secondary tab bar controller modally using a crossfade transition.



来源:https://stackoverflow.com/questions/19552682/tabs-do-not-show-when-using-pushviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!