I have an IOS app with a UITabBar and have its delegate set to my class.. the didSelectTabBarItem properly fires and all is right with the world. However I do have some conditional code that has to occur when the UITabBarItem selected is after one particular UITabBarItem IE.. if the user clicks on tab bar item 3, and they were currently on tab bar item 2 I have to do a little extra code, that I would not have to do if the user selected tab bar item 3 and was previously on tab bar item 1.
So, is there anyway programmatically (other than keeping direct track via my program via a state variable, to know what was the previously selected item was on a tab bar when a new tab bar item is selected?
Yes it is possible, through key-value-observing (
KVO).note This answer is in regard to a
UITabBarnot aUITabBarController. Tab bar controller delegates have methods you are looking for (as mentioned by rdelmar).To start, observe your tab bar like so:
I think you can already see where I’m going based on my using both options old & new. Then simply observe the change instead of using the delegate method, like so:
Remember to remove yourself as observer in both
viewDidUnloadanddealloc, sinceviewDidUnloadmay never be called.