Why this way to set the tab bar item badge doesn't work?

Discussion in 'iOS Programming' started by pan17, Jul 12, 2012.

  1. pan17, Jul 12, 2012
    Last edited by a moderator: Jul 12, 2012

    pan17 macrumors newbie

    Joined:
    Jul 9, 2012
    #1
    I've tried this way in a TableViewController inside a TabBarController :

    Code:
    self.tabBarItem.badgeValue = @"AB";
    Then the tab bar item badge didn't change when i run the code.

    When i use this code:

    Code:
    [[[[self tabBarController] tabBar] items] objectAtIndex:0] setBadgeValue:@"AB"];
    Then it works, why?

    You have to change its badge value by accessing the tarBar items array?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Are you sure self.tabBarItem matches [[[self tabBarController] tabBar] items] objectAtIndex:0]?
     
  3. pan17 thread starter macrumors newbie

    Joined:
    Jul 9, 2012
    #3
    Yeah, cause i call self.tabBaritem inside the tableViewController which is the first tab of the TabBarController. At least i think it should be,right?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Why guess? Verify it.
     
  5. pan17 thread starter macrumors newbie

    Joined:
    Jul 9, 2012
    #5
    Ok, i tried this:
    Code:
    if ([[[[[self tabBarController] tabBar] items] objectAtIndex:0] isEqual:self.tabBarItem]) {
                NSLog(@"Hello");
            }
            NSLog(@"%@",[[[[self tabBarController] tabBar] items] objectAtIndex:0]);
            NSLog(@"%@",self.tabBarItem);
    Then got these output:
    Code:
    2012-07-13 10:01:49.532 Flicker[2725:f803] <UITabBarItem: 0x6a53310>
    2012-07-13 10:01:49.533 Flicker[2725:f803] <UITabBarItem: 0x6c708e0>
    So, they are not the same. But why?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    How (and where) is self.tabBarItem assigned?
     
  7. pan17 thread starter macrumors newbie

    Joined:
    Jul 9, 2012
    #7
    Ah, i kinda get it.
    So, the self is a UITableViewController, so self.tabBarItem is not the same as [[[self tabBarController] tabBar] items] objectAtIndex:0]. So, in this case, i have to get the tabBarController first then get the tabBar items then get the corresponding tabBarItem it has,right?
    What i thought is:self.tabBarItem should be the tabBarItem inside that UITableViewController, but it turns out incorrect. But what is the self.tabBarItem??
    Thanks
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Did you check the documentation?
     
  9. pan17, Jul 15, 2012
    Last edited by a moderator: Jul 15, 2012

    pan17 thread starter macrumors newbie

    Joined:
    Jul 9, 2012
    #9
    Yeah, the doc says this which i don't understand much.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    This property sounds like what you want then. Hmm...

    And I find it curious that your self.tabBarItem was non-nil (which I was expecting to be; nil, that is). Have you tried checking it against all the tabBar items? I'm wondering if it matches one of the other tabs.
     
  11. pan17 thread starter macrumors newbie

    Joined:
    Jul 9, 2012
    #11
    nah, I didn't. Thank you,though.
     

Share This Page