Tab bar without TabBarController - add View Controller for Tab bar item in storyboard

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 21:56:38

问题


I have added a Tab bar (not a TabViewController) to a View Controller and then added some Tab bar items to that Tab bar.

Now I want to attach other View Controllers to those tab bar items in Storyboard.

When I do Ctrl + Drag to View Controller from tab bar item I do not get any options.

Please suggest a way to do this.


回答1:


I had the same problem, but i couldn't find a way to assign to a viewController its own viewControllers as in the TabViewController case.

I solved it using containers. One contarner for each tabBarItem in your tabBar, which are hidden or showed depending of the selected tabBarItem in the tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method.

1. Create your containers in your UIviewController in storyBoard: Just like this Select your tabBar and Ctrl+Drag to delegate the class for listen the tabBarDelegate methods: look here

2. Declare the corrisponging IBOutlets, incliding your tabBAr:

#import <UIKit/UIKit.h>

@interface TabsMainViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITabBar *tabBar;
@property (strong, nonatomic) IBOutlet UIView *directoryContainer;
@property (strong, nonatomic) IBOutlet UIView *groupsContainer;
@end

3. Select the container to show in the tabBarDelegate method:

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

            switch (item.tag) {
            case 1:
                _directoryContainer.hidden = NO;
                _groupsContainer.hidden = YES;
             break;

            case 2:
                _directoryContainer.hidden = YES;
                _groupsContainer.hidden = NO;
                break;

            default:
                break;
        }

    }

Hope that helps!



来源:https://stackoverflow.com/questions/36783074/tab-bar-without-tabbarcontroller-add-view-controller-for-tab-bar-item-in-story

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