2011-05-24 45 views
0

我有一個UIViewController。我使用navigationItem的rightBarButtonItem作爲「重新加載按鈕」。當按下重新加載按鈕時,我使用leftBarButtonItem在應用程序正在處理要重新加載的數據時顯示活動指示器。一切都很好,直到這裏。
下面是一些代碼:更改navigationItem的barButtonItem

- (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; 
} 

雖然它似乎是工作(Info按鈕是存在的,當我按下「刷新」的activityIndi​​cator取代其位置,直到處理完成),我得到了下面的調試器控制檯:

__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

您使用後臺線程來處理你的數據? – Hisenberg 2011-05-24 14:39:16

回答

0

我的猜測是你正在後臺線程中運行「重新加載」任務,當它完成時,你從後臺線程中調用spinEnd。如果後臺線程沒有適當的自動釋放池,您將看到這些警告。

既然你應該永遠只調用從主線程UIKit方法,你應該做調用spinEnd

[self performSelectorOnMainThread:@selector(spinEnd) withObject:nil waitUntilDone:NO]; 
+0

你好@Daniel謝謝你的回覆。我使用另一個線程來啓動微調器。如果我使用相同的線程,微調不顯示。這是重新加載方法: ' - (IBAction)refreshFeeds:(id)sender { \t if(stories){ \t \t \t [stories release]; \t \t stories = nil; \t} \t [self parseXMLFileAtURL:_theUrl];我試着用[[NSThread detachNewThreadSelector:@selector(spinBegin)toTarget:self withObject:nil];'和NSXMLParser的parserDidEndDocument'方法啓動spinner,我使用'[NSThread detachNewThreadSelector:@選擇器(spinEnd)toTarget:self withObject:nil];'停止它。 – CrisDeBlonde 2011-05-24 14:52:50

+0

如果我使用你的建議(既啓動和停止微調)它永遠不會出現。我錯過了什麼? – CrisDeBlonde 2011-05-24 14:53:43

+0

它可能不會顯示,因爲您通過阻止主線程同步下載和解析XML,所以UIKit永遠不會有機會執行任何繪圖(如顯示微調器)。你應該做的是正常地啓動微調器,然後在後臺線程上進行下載/解析,然後當完成時,回到主線程以停止微調器。 – 2011-05-24 14:57:07

0

您的房產是retain ed?你正在做和alloc] init...但分配給你的屬性後不會釋放它們。我建議檢查出Memory Management Guide