Saurabh Hooda
Saurabh Hooda

Reputation: 2596

UIAlertView causing change to navigation bar button's titleLabel

I am observing a weird trend in my app: whenever an alert message is displayed, the value of navigation bar button's title gets changed to its storyboard title value. Here is one of the UIAlertView:

navigation bar button title getting changed by alert view

Title value 100 that is visible in image above is the value that is present in my storyboard. I am successfully changing this bar button's value to whatever is the current score with this line:

self.coinsRemainingButton.titleLabel.text = self.coinsRemaining;  

Everything works fine until I display some alert view. As soon as any UIAlertView is displayed, this navigation bar button's title gets changed to 100 irrespective of the current score. For ex. even if my current score is 500, as soon as an alert message appears my bar button's value gets changed to 100, as shown in screenshot above.
I've checked thoroughly and I am not changing the value mistakenly myself.
If I remove UIAlertView lines everything comes back in order.
Thanks for your time.

Update 1: Here is my UIAlertView code:

 UIAlertView *iapSuccessful = [[UIAlertView alloc] initWithTitle:@"Purchase Successful"
                                                            message:@"Congrats!! You just got coins"
                                                           delegate:Nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles: nil];
 [iapSuccessful show];

Update 2: I couldn't find solution so for the sake of workaround I thought of updating my bar button title by code immediately after UIAlertView is displayed:

[iapSuccessful show];
self.coinsRemainingButton.titleLabel.text = self.coinsRemaining;

but UIAlertView is still forcibly changing title of the bar button to storyboard value.

Upvotes: 4

Views: 1349

Answers (2)

Saurabh Hooda
Saurabh Hooda

Reputation: 2596

I tried several tricks to solve this problem but as soon as any UIAlertView appears, value (titleLabel.text) of the bar button item gets changed to its storyboard value.

I observed that value is getting changed only for the navigation bar objects that are drawn from storyboard. So I removed bar button item from storyboard and added the bar button programmatically and it solved my problem :)
Here is code for adding bar button item programmatically, if you require:

//Add a custom button as the right bar button item of navigation bar
self.coinsRemainingButton.frame = CGRectMake(0.00f, 0.00f, 72.00f, 37.00f);
[self.coinsRemainingButton setBackgroundImage:[UIImage imageNamed:@"coinsBackground"] forState:UIControlStateNormal];
[self.coinsRemainingButton setTitle:@"100" forState:UIControlStateNormal];
UIBarButtonItem *rightCustomBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.coinsRemainingButton];
self.navigationItem.rightBarButtonItem = rightCustomBarButtonItem;

Upvotes: 2

Ilario
Ilario

Reputation: 6079

removing the title of the button from the storyboard, it solves the problem

Upvotes: 1

Related Questions