0

My iOS app uses a tab bar controller, and when a user taps the "record" icon uitabbaritem in the tab bar, I want the icon image to change and audio to start recording straight away, then when the new "record" image is tapped, I want the image to revert back to the original image. I am having trouble implementing both the image switch and starting the audio recording because I don't know how to properly access the uitabbaritem or the tabbarcontroller.

In Swift, how do I access the uitabbaritem so I can perform these actions?

FaceRake
  • 3
  • 3

2 Answers2

0

You can do as below to assign image to tab bar.

  let myTab = self.tabBarController!.viewControllers?[0].tabBarItem as UITabBarItem! //get the desire tab by passing index as 0,1,2,3... Currently i am pointing to first tab.
 myTab.image = UIImage(named: "image1")//This image will appear when tab is not selected
 myTab.selectedImage = UIImage(named: "image2")//This image will appear when tab is selected

Now to check wether you are clicking same tab or not, implement the delegate method,

    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
          if viewController.tabBarItem.tag == 0 { // assuming this is your desired so called record tab
               if !self.isRecordSelected {// create a property isRecordSelected of Bool type

                  tabBarItem.selectedImage = UIImage(named: "new record image")//This image will appear when tab is recording starts.
                  self.isRecordSelected = true
               } else {
                  tabBarItem.selectedImage = UIImage(named: " revert image")//This image will appear when tab is recording stops.
                  self.isRecordSelected = false
              }
         }
     }

Give tag number to each tab, so that it will be easy for identification. Set the UITabBarDelegate delegate in your class.

Amit89
  • 3,000
  • 1
  • 19
  • 27
  • This helps and I follow your logic. I'm getting an error that my tabBarController does not have a member named isRecordSelected...any idea how to fix? – FaceRake May 08 '15 at 23:35
  • This will be a property in your class, so create this bool property.The error telling that you have not declared this property and give initial value as false. – Amit89 May 09 '15 at 02:08
0

Presumably you'll want to show some kind of UI while recording, correct? You could simply allow the tab bar item to perform its usual function and switch to/load a view controller. In the view controller, immediately kick off recording.

bgilham
  • 5,909
  • 1
  • 24
  • 39