2011-05-26 92 views
0

我有一個UITableview頂部的導航欄。我有一個刷新按鈕作爲rightBarButtonItem。延遲隱藏導航右欄按鈕項

當刷新按鈕被點擊時,我想隱藏刷新按鈕,重新加載表格並顯示一個alertview。

-(void)refreshClicked{ 
    self.navigationItem.rightBarButtonItem=nil; 
    app.networkActivityIndicatorVisible = YES; 
    [appDelegate readJSONData]; 
    [self.tableView reloadData]; 
    UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [infoAlert show]; 
    [infoAlert release]; 
} 

我發現當我的wifi信號變弱時,刷新按鈕不會立即隱藏,並且存在延遲。我擔心,如果使用3G,會有進一步的延遲,用戶可能會再次按下刷新按鈕,並認爲第一次沒有按下按鈕。

我的代碼有問題嗎?

幫助,將不勝感激

編輯-----------

-(void)refreshClicked{ 
    self.navigationItem.rightBarButtonItem=nil; 
    app.networkActivityIndicatorVisible = YES; 

    // do data processing in the background 
    [self performSelectorInBackground:@selector(doBackgroundProcessing) withObject:self]; 

    UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [infoAlert show]; 
    [infoAlert release]; 
} 


- (void)doBackgroundProcessing { 
    NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init]; 
    [appDelegate readJSONData]; 

    // must update the UI from the main thread only; equivalent to [self.tableView reloadData]; 
    [self performSelectorOnMainThread:@selector(reloadData) withObject:self.tableView waitUntilDone:NO]; 

    [pool release]; 
} 

錯誤

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[campaignTableViewController reloadData]: unrecognized selector sent to instance 0x703eba0' 

回答

0

基本上,雖然你無rightBarButtonItem,在控制返回到應用程序的主運行循環之前,該更改不會反映在UI中。因此,如果您的方法的其餘部分需要一些明顯的時間(就像網絡請求一樣),那麼在完成該工作之後,您將看不到該按鈕。

更直接:你阻止主線程;要修復它,您需要在後臺線程上執行耗時的工作。

像這樣的東西應該工作(既不編譯,也不測試):

-(void)refreshClicked{ 
    self.navigationItem.rightBarButtonItem=nil; 
    app.networkActivityIndicatorVisible = YES; 

    // do data processing in the background 
    [self performSelectorInBackground:@selector(doBackgroundProcessing) withObject:self]; 

    // go ahead and show the alert immediately 
    UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [infoAlert show]; 
    [infoAlert release]; 
} 

- (void)doBackgroundProcessing { 
    [appDelegate readJSONData]; 

    // must update the UI from the main thread only; equivalent to [self.tableView reloadData]; 
    [self performSelectorOnMainThread:@selector(reloadData) withObject:self.tableView waitUntilDone:NO]; 
} 
+0

感謝您的回覆時完成。我是iphone開發新手。你能告訴我一個例子嗎? – Neelesh 2011-05-26 20:44:51

+0

除非你打算換掉另一個按鈕,否則你可能應該採納@PenOne的建議。 – bosmacs 2011-05-26 21:42:47

+0

這是我得到的錯誤。 *** __NSAutoreleaseNoPool():類NSURL的對象0x9c06910自動釋放,沒有池到位 - 只是漏水 – Neelesh 2011-05-26 23:10:53

0

爲什麼不禁止的刷新按鈕來代替,它容易得多,而且個人是我會假定用戶期望的那樣。它是衆多軟件應用程序中使用的範例。如果我觸摸一個按鈕,我真的希望它消失,爲什麼它消失了,它是否壞了?如果它被禁用,用戶可以看到某些事情正在發生(活動指示燈,提示框 - 也許矯枉過正?),那麼他們會更自信,你的應用程序在一個可預見的和可靠的方式

myButton.isEnabled = NO; 

//一套行爲當你做你的東西活動的指標

//後來

myButton.isEnabled = YES;