我面臨着一個非常奇怪的行爲UIBarButtonItem
。iPhone SDK:內存警告後UIBarButtonItem不刷新
我已經能夠在代碼被粘貼在下面的測試應用程序中重現該錯誤。
在我的第一個視圖中,我有一個顯示GUID的標籤,轉到空的第二個視圖的按鈕以及UIBarButtonItem
。如果點擊它,顏色會從綠色變爲紅色,反之亦然,並且GUID更新。
下面是如何重現它:
- 轉到第二個觀點
- 提高內存警告(在仿真器硬件菜單,但工作原理是一樣與真正的存儲設備上的警告)
- 回到第一個
- 點擊
UIBarButtonItem
的UIBarButtonItem
不是R即使它在之前和日誌顯示一切似乎正確efreshing。
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
而且情節提要:
它會影響5.1.1模擬器5.0和5.1,以及設備
編輯:通過記錄self.aButton的值我看到它成爲nil
後內存警告,由於weak
關鍵字。但我如何重新分配? SDK不應該自己做?爲什麼aGUID
仍然可用?
編輯:這裏是一個日誌。我們可以看到aButton變成零。
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
有沒有人有想法? – dvkch 2012-07-16 13:16:37