我面临UIBarButtonItem
的非常奇怪的行为。
我已经能够在其代码已粘贴到下面的测试应用中重现该错误。
在我的第一个视图上,我有一个显示GUID的标签,一个用于转到第二个空视图的按钮和一个UIBarButtonItem
。如果单击它,其颜色将从绿色变为红色,反之亦然,并且GUID更新。
复制方法如下:
转到第二个视图
发出内存警告(在“模拟器硬件”菜单中,但与设备上的实际内存警告相同)
回到第一个
点击UIBarButtonItem
UIBarButtonItem
即使在以前也不刷新,并且日志显示一切正常。
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.0和5.1,以及5.1.1上的设备
编辑:通过记录self.aButton的值,我看到内存警告后由于
nil
关键字,它变为weak
。但是我该如何重新分配呢? SDK不应该自己做吗?为什么aGUID
仍然可用?编辑:这是一个日志。我们可以看到aButton变为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
最佳答案
内存警告后,refreshView
是否被调用?
尝试删除将viewDidUnload
中的视图设置为nil的代码?它能解决问题吗?
关于iphone - iPhone SDK:内存警告后,UIBarButtonItem不刷新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11449867/