Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:25:38+00:00 2026-06-11T15:25:38+00:00

I have an IOS app with a UITabBar and have its delegate set to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:25:39+00:00Added an answer on June 11, 2026 at 3:25 pm

    Yes it is possible, through key-value-observing (KVO).

    note This answer is in regard to a UITabBar not a UITabBarController. Tab bar controller delegates have methods you are looking for (as mentioned by rdelmar).

    To start, observe your tab bar like so:

    - (void)viewDidLoad{
        [super viewDidLoad];
        [self.tabBar addObserver:self forKeyPath:@"selectedItem" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    }
    

    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:

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
        if ([keyPath isEqualToString:@"selectedItem"] && [object isKindOfClass:[UITabBar class]]){
            UITabBar *bar = (UITabBar *)object; // The object will be the bar we're observing.
            // The change dictionary will contain the previous tabBarItem for the "old" key.
            UITabBarItem *wasItem = [change objectForKey:NSKeyValueChangeOldKey];
            NSUInteger was = [bar.items indexOfObject:wasItem];
            // The same is true for the new tabBarItem but it will be under the "new" key.
            UITabBarItem *isItem = [change objectForKey:NSKeyValueChangeNewKey];
            NSUInteger is = [bar.items indexOfObject:isItem];
            NSLog(@"was tab %i",was);
            NSLog(@"is tab  %i",is);
        } 
        // handle other observings.
    }
    

    Remember to remove yourself as observer in both viewDidUnload and dealloc, since viewDidUnload may never be called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an iOS app which has been approved for release with its version
I have an iOS app where I set the Required Device Capabilities config setting
I have an iOS-App which uses ARC. I don't use InterfaceBuilder, all UI is
I have an iOS app where all I want to do is provide more
I have an iOS app in which every time the view is loaded, all
I have an iOS app that gets all the videos from a user through
I have an iOS app sending requests to a PHP based web service ,
I have an iOS app that uses a number of enums for valid values,
I have an iOS app with a UITableView that has flexible width, which allow
I have an iOS app and I want to test it on simulator. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.