How to associate a tab bar item with an action?

I have a UIView that has a UITabBar with 4 UITabBarItem components (they are all created from IB).

I want my IBAction function to be called when someone clicks on items in a tab bar. But I canโ€™t connect the tabbaritem to my action via IB ... I control the drag from the "accepted actions", but this does not allow me to connect it to the tabbaritem.

Thanks Deshawn

+6
source share
3 answers

From your question, it is unclear whether you have also defined a UITabBarController.

If you havenโ€™t done this (as I believe, otherwise clicking on the tab bar item should work if you defined things correctly in IB), the transition method is to assign UITabBarDelegate to your UITabBar and define tabBar:didSelectItem:

Check out the link for UITabBarDelegate

+5
source

Manage, drag and drop your tab into your โ€œfile ownerโ€ in IB and set it as a delegate. Then remove this code in the viewcontroller.m file:

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if(item.tag == 0) { //party like its 1999 right here } } 

Go back to IB and set each element of the tab bar in the tab bar with a tag. You should check every case in your didSelectItem file, so if you have several different tags, etc. The way I did it.

+14
source

You cannot set the target action in tab bar items. They should always have a new view. The only way is that you must use delegates that determine that your tab item is affected, and then process which tab is clicked on this deletion.

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if(item.tag == 1) { } } 

You must set the tag value to your tab element from properties in xcode.

0
source

Source: https://habr.com/ru/post/893996/


All Articles