2

我面臨着一個非常奇怪的行爲UIBarButtonItemiPhone 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 

而且情節提要:

storyboard

它會影響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 
+0

有沒有人有想法? – dvkch 2012-07-16 13:16:37

回答

1

refreshView在內存警告後被調用?

嘗試刪除將視圖設置爲無的代碼viewDidUnload?它解決了問題嗎?

+0

refreshView被調用。我在我的問題中添加了一個日誌。並確實從viewDidUnload刪除行做了詭計,但這很奇怪是不是?爲什麼aGUID總是在工作,而不是aButton?如果按鈕處於實際視圖中,那就沒問題了,那爲什麼他們有區別呢?是不是某種錯誤? – dvkch 2012-07-19 12:42:44

+0

我不知道。儘管這不是同一個標籤。從第二個控制器彈出後,似乎標籤被重新創建,但不是酒吧按鈕。 – htinlinn 2012-07-19 17:22:50