How do I connect UITabBar items from a UITabBar (not a UITabBarController) to different views in the IB?

[亡魂溺海] 提交于 2020-01-15 05:05:29

问题


I’ve created a StoryBoard project and added a UITabBar to my first view and a few UITabBar items on it. I’m using a UITabBar and not a UITabBarController because the UITabBar needs to be scrollable vertically so it is inside a UIScrollView. I want to connect the UITabBar items to different ViewControllers. How do I do that inside the Interface builder?

With UITabBarController it’s just a Ctrl+drag like everything else in the IB, but for some reason the UITabBar acts differently. I’m aware of all the delegation methods I need to implement, but for now I’m only interested on how to connect the UITabBar items to the views.


回答1:


What you're probably after is UITabBarController.

You generally don't want to manage a UITabBar manually.

If you're using storyboards, you can drag out a UITabBarController and then Ctrl-drag from it to other view controllers in the storyboard to link them to the viewControllers property.




回答2:


Try:

firstViewController.tabBarItem = firstItem;



回答3:


  1. From UITabBar item ctr+drag to your next ViewController (select style push, modal).
  2. Give you UITabbarItem tags.
  3. Import UITabBarDelegate to your Controller.
  4. Add following delegate method

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
    {
        if (item.tag==0) {
            [self performSegueWithIdentifier:@"Favorite" sender:nil];
        }
        else if (item.tag==1)
        {
           [self performSegueWithIdentifier:@"Item" sender:nil];
        }
    }
    
    1. Give identifier for each segue.

you can get my demo from following link

but problem is that tabbar will not appear on next ViewController



来源:https://stackoverflow.com/questions/16962043/how-do-i-connect-uitabbar-items-from-a-uitabbar-not-a-uitabbarcontroller-to-di

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