开发者

BarButtonItem's Title Not Updating

I use this code throughout my project to update the title of my UIBarButtonItem.

In this particular class, I have an NSFetchedResultsController which I want to to get the count for to set as the button's title.

I am calling this code in ViwDidLoad but it seems as though it is being called before the fetch, therefore the count is always 0. I tried putting it at the end of the Fetch delegate method, but that didn't help either. Any ideas? When I NSLog the count in the fetch delegate method, it is correct.

self.myBarButton.title = [NSString stringWithFormat: @"%i count", [fetchedResultsController.fetchedObjects count]];

viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (managedObjectContext == nil) 
    { 
        self.managedObjectContext = [(TestAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    }

    NSSet *filteredFromSession=[selectedSession.property filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@",name]];
    if ([filteredFromSession count] > 0)
    {
        self.exercise = [filteredExercisesFromSession anyObject];
    }
    se开发者_运维技巧lf.setLabel.title = [NSString stringWithFormat: @"%i sets", [fetchedResultsController.fetchedObjects count]];
}


It's most probable that at the time you do this,

self.setLabel.title = [NSString stringWithFormat: @"%i sets", [fetchedResultsController.fetchedObjects count]];

fetchedResultsController is nil.

If you've a getter method defined for fetchedResultsController and have declared it as a property, I suggest you do the fetch before your line above,

NSError * error;
if (![self.fetchedResultsController performFetch:&error]) {
    // Error
}

Basically ensure that the fetch is completed prior to setting the title. Once you do this, you might want to remove the fetch from the place you're doing right now.


Have you checked "self.myBarButton" to make sure it's not nil at the time you're setting the title? (Like you forgot to set the outlet properly, etc)?


How you are accessing bar button item? how about accessing like this.

BarButtonItem * item = [self.navigationItem rightBarButtonItem];
[itme setTitle:[NSString stringWithFormat: @"%i count", [fetchedResultsController.fetchedObjects count]]


Try this

self.setLabel.title = [NSString stringWithFormat: @"%d sets", [self.fetchedResultsController.fetchedObjects count]];

in place of

self.setLabel.title = [NSString stringWithFormat: @"%d sets", [fetchedResultsController.fetchedObjects count]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜