简体   繁体   中英

iOS - incorrect badge value retrieved from tab

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];
        }
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM