How do I connect UITabBar items from a UITabBar (n

2019-07-22 00:20发布

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.

3条回答
▲ chillily
2楼-- · 2019-07-22 00:51
  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

查看更多
Evening l夕情丶
3楼-- · 2019-07-22 00:53

Try:

firstViewController.tabBarItem = firstItem;
查看更多
叼着烟拽天下
4楼-- · 2019-07-22 01:08

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.

查看更多
登录 后发表回答