0

I'm trying to retrieve the value displaying for a specified tab. It was working before and it stopped for some reason? The int current_badge = line on the code below is where the badge value is fetched and returns zeros no matter what the badge value actually is. I'm in a viewcontroller segued off of a uitabcontroller. Anyone have any thoughts on what I'm doing wrong?

UPDATE: It appears to be because I'm in a viewcontroller off of a tab controller. Move the same code into a tab viewcontroller and it works fine. Is there a better way to determine the badge value in a tabcontroller when segued out of it?

-(void)badgeUpdate:(int)tab:(int)dayspan
{
    // getting the current badge amount
    int current_badge = [[[super.tabBarController.viewControllers objectAtIndex:tab] tabBarItem].badgeValue intValue];

    // testing for badge level
    if (current_badge > 0)
    {
        // testing for updates on tab for dayspan or longer
        int updates_waiting = [self updatesWaitingCheck:tab:dayspan];
        if (updates_waiting > 0)
        {
            // setting new badge level on tab
            [[[[[self tabBarController] viewControllers] objectAtIndex:tab] tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", updates_waiting]];
        }
        else
        {
            // turning off badge display
            [[[[[self tabBarController] viewControllers] objectAtIndex:tab] tabBarItem] setBadgeValue:nil];
        }
    }
}
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
Grymjack
  • 529
  • 1
  • 10
  • 21

1 Answers1

1

The controller for that tab is likely being deallocated when you switch away. I would suggest moving the logic to determine the current count out of the view layer, and into a higher level controller that persists across all tabs. Otherwise, you'll run into the problem you're seeing.

Most of the time, if you're looking for a data value in a view object, rather than in a controller or model somewhere, you're working against the framework and you'll run into problems like this.

Jeremy Massel
  • 2,257
  • 18
  • 22
  • I have the TabBarController class where this code works fine. It stops working when I do a modal transition to the screen. When I exit this screen back to the tab controller it works fine. It isn't deallocated because when I unwind the segue I return back into the tab controller without any problems. So I guess my question is how can I get the badge value of a tab when I am segued into another view? – Grymjack Mar 03 '13 at 17:13
  • 1
    The controller could still be deallocated in a low-memory condition, then recreated from a serialized state; similar to how fast app switching works. Whether the inactive controller remains allocated could change based on which device it is running on, which version of iOS, and even whether the user is playing music in the background. That's why its best to store this data in high-level persistent object like your application delegate. (Note that I'm not _suggesting_ the app delegate is the place for this for this - the organization of this is up to you) – Jeremy Massel Mar 03 '13 at 17:21
  • Seems strange the code wouldn't throw an access error trying to access the badge value then? So, would your suggestion then be to move this code back into the tab controller and run it upon exit from the segue? – Grymjack Mar 03 '13 at 17:27