Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Adding a play button to TabBar

Hi, how would i make a tabBar item play an audio file and NOT move to a different scene. This is for iOS with Swift.

1 Answer

Hi Iffat,

A way I would approach this would be using UITabBarControllerDelegate methods to prevent the viewcontroller from changing. Specifically, - tabBarController:shouldSelectViewController: method could prevent moving to a different scene.

To play the music, I would put the logic in the - tabBarController:didSelectViewController: method or using UITabBarDelegate method of tabBar:didSelectItem:.

Here's a description for UITabBarControllerDelegate according to Apples documentation:

"You can use it to determine whether specific tabs should be selected, to perform actions after a tab is selected, or to perform actions before or after the user customizes the order of the tabs"

Hope this helps! :)