我有一个UIViewController。我使用navigationItem的rightBarButtonItem作为“重新加载按钮”。当按下重新加载按钮时,我使用leftBarButtonItem显示活动指示器,同时应用程序正在处理要重新加载的数据。这里一切都很好。
这是一些代码:
- (void)initSpinner {
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityButton = [[UIBarButtonItem alloc] initWithCustomView: activityIndicator];
self.navigationItem.leftBarButtonItem = activityButton;
}
- (void)spinBegin {
[activityIndicator startAnimating];
}
- (void)spinEnd {
[activityIndicator stopAnimating];
}
- (void)viewDidLoad {
[self initSpinner];
[super viewDidLoad];
//more stuff going on here....
}
-(void) loadView{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshFeeds:)];
}
现在我被要求改变一下逻辑
rightBarButtonItem保持原样。 leftBarButtonItem仍然显示活动指示器,但是当没有处理时,它应该显示另一个按钮,按下时将显示一个新视图(对于某些信息)。
所以我做了这些改变:
- (void)initSpinner {
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityButton = [[UIBarButtonItem alloc] initWithCustomView: activityIndicator];
}
- (void)spinBegin {
self.navigationItem.leftBarButtonItem = activityButton;
[activityIndicator startAnimating];
}
- (void)spinEnd {
[activityIndicator stopAnimating];
self.navigationItem.leftBarButtonItem =infoBarButton; //= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshFeeds:)] autorelease];
}
-(void) loadView{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshFeeds:)];
if (infoBarButton == nil){
UIImage *buttonImage = [UIImage imageNamed:@"info.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, 25, 25);
// Initialize the UIBarButtonItem
infoBarButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
// Set the Target and Action for aButton
[aButton addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
}
self.navigationItem.leftBarButtonItem = infoBarButton;
}
虽然它似乎正在工作(信息按钮就在那里,当我按下“重新加载”时,activityIndicator取代它直到处理完成),我在调试器控制台中得到以下内容:
__NSAutoreleaseNoPool(): Object 0x15ef30 of class UINavigationItem autoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): Object 0x1abd90 of class UIBarButtonItem autoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): Object 0x1acb20 of class UIButton autoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): Object 0x15ef30 of class UINavigationItem autoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): Object 0x1ad030 of class UIActivityIndicatorView autoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): Object 0x1ad030 of class UIActivityIndicatorView autoreleased with no pool in place - just leaking
__NSAutoreleaseNoPool(): Object 0x1a8c40 of class UINavigationButton autoreleased with no pool in place - just leaking
Object 0x941c of class NSCFString autoreleased with no pool in place - just leaking
//MORE similar messages following...
我做错了什么?我希望那里有人可以提供帮助...
提前谢谢。
答案 0 :(得分:0)
您的媒体资源是否为retain
?您正在执行alloc] init...
,但在将它们分配给您的属性后不会释放它们。我建议查看Memory Management Guide
答案 1 :(得分:0)
我的猜测是你在后台线程中运行“重新加载”任务,当它完成时,你从后台线程中调用spinEnd
。如果后台线程没有自动释放池,您将看到这些警告。
由于您只应该从主线程中调用UIKit
方法,因此您应该通过执行以下操作来调用spinEnd
:
[self performSelectorOnMainThread:@selector(spinEnd) withObject:nil waitUntilDone:NO];