I'm facing a very strange behavior of UIBarButtonItem
.
I have been able to reproduce the bug in a test app whose code has been pasted below.
On my first view I have a label displaying a GUID, a button to go to an empty second view, and a UIBarButtonItem
. If I click it its color changes from green to red and vice versa and the GUID updates.
Here is how to reproduce it:
UIBarButtonItem
The UIBarButtonItem
is not refreshing even if it was before and logs show everything seems correct.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
@private
BOOL _isGreenNotRed;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *aButton;
@property (weak, nonatomic) IBOutlet UILabel *aGUID;
@end
ViewController.m
#import "ViewController.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
@interface ViewController ()
- (NSString *)generateUUID;
- (void)refreshView;
@end
@implementation ViewController
@synthesize aButton;
@synthesize aGUID;
- (NSString *)generateUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return (__bridge_transfer NSString *)string;
}
-(void)refreshView {
NSLog(@"Refreshing first view");
if(self->_isGreenNotRed) {
[self.aButton setTintColor:[UIColor colorWithRed:0. green:0.8 blue:0. alpha:1.]];
NSLog(@"Button should be green");
}
else {
[self.aButton setTintColor:[UIColor colorWithRed:0.8 green:0. blue:0. alpha:1.]];
NSLog(@"Button should be red");
}
self->_isGreenNotRed = !self->_isGreenNotRed;
NSString *guid = [self generateUUID];
[self.aGUID setText:guid];
NSLog(@"GUID should be %@", guid);
}
- (IBAction)aButtonClick:(id)sender {
[self refreshView];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self->_isGreenNotRed = YES;
[self refreshView];
}
- (void)viewDidUnload
{
[self setAButton:nil];
[self setAGUID:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"Going to second view");
}
@end
And the storyboard :
It affects Simulator 5.0 and 5.1, and Device on 5.1.1
EDIT : by logging the value of self.aButton I saw it became nil
after the memory warning, due to the weak
keyword. But how can I reassign it ? Shouldn't the SDK do it by itself ? And why is aGUID
still available ?
EDIT : here is a log. we can see aButton becomes nil.
2012-07-19 14:39:14.716 test[934:f803] Refreshing first view
2012-07-19 14:39:14.718 test[934:f803] Button <UIBarButtonItem: 0x68b89d0> should be green
2012-07-19 14:39:14.719 test[934:f803] GUID label <UILabel: 0x6dbe580; frame = (49 291; 222 48); text = '08960AFD-F98A-4FBA-9A6E-C...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6dbe5f0>> should be 08960AFD-F98A-4FBA-9A6E-C548E753F259
2012-07-19 14:39:18.690 test[934:f803] Refreshing first view
2012-07-19 14:39:18.692 test[934:f803] Button <UIBarButtonItem: 0x68b89d0> should be red
2012-07-19 14:39:18.692 test[934:f803] GUID label <UILabel: 0x6dbe580; frame = (49 291; 222 48); text = 'EBB41B52-46D1-4CAE-BF0E-B...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6dbe5f0>> should be EBB41B52-46D1-4CAE-BF0E-B0679BC1F0EE
2012-07-19 14:39:19.946 test[934:f803] Refreshing first view
2012-07-19 14:39:19.949 test[934:f803] Button <UIBarButtonItem: 0x68b89d0> should be green
2012-07-19 14:39:19.949 test[934:f803] GUID label <UILabel: 0x6dbe580; frame = (49 291; 222 48); text = '8E802026-E825-4219-9CCA-5...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x6dbe5f0>> should be 8E802026-E825-4219-9CCA-5106BF65BDA0
2012-07-19 14:39:33.353 test[934:f803] Going to second view
2012-07-19 14:39:40.811 test[934:f803] Received memory warning.
2012-07-19 14:39:43.471 test[934:f803] Refreshing first view
2012-07-19 14:39:43.472 test[934:f803] Button (null) should be green
2012-07-19 14:39:43.473 test[934:f803] GUID label <UILabel: 0x6896570; frame = (49 291; 222 48); text = '3D4933A8-908B-4570-BD55-6...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x68b0130>> should be 3D4933A8-908B-4570-BD55-68C298F6E621
2012-07-19 14:39:48.649 test[934:f803] Refreshing first view
2012-07-19 14:39:48.650 test[934:f803] Button (null) should be red
2012-07-19 14:39:48.651 test[934:f803] GUID label <UILabel: 0x6896570; frame = (49 291; 222 48); text = '9CDCF065-12C2-4F16-9464-A...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x68b0130>> should be 9CDCF065-12C2-4F16-9464-A5CE202BE7CA
Is refreshView
getting called after the memory warning?
Try removing the code that sets the views to nil in viewDidUnload
? Does it solve the problem?