2012-10-14 92 views
0

我的應用程序啓動時加載了JSON。MBProgressHUD在重新加載數據時未顯示

當數據加載時,MBProgressHUD正確顯示微調器。

我也有一個觸發重新加載JSON的刷新按鈕 - 我希望它顯示微調。雖然數據刷新,但微調不顯示。

任何想法我做錯了什麼?

這裏是我ViewController.m相關代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    [self fetchPosts]; 
} 

- (IBAction)refresh:(id)sender { 
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; // NOT WORKING 
    [self refreshPosts]; 
} 

- (void)fetchPosts 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://mysite.com/app/"]]; 

     NSError* error; 

     posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.collectionView reloadData]; 
     }); 
    }); 
} 

- (void)refreshPosts 
{ 
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://mysite.com/app/"]]; 

    NSError* error; 

    posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.collectionView reloadData]; 
    }); 
} 
+0

您是否嘗試在調度塊中放置refreshPosts的整個代碼(而不僅僅是調用reloadData)? – Ravi

+0

@ravi這樣做會產生一組單獨的問題,其中刷新操作不響應點擊刷新按鈕 - 您認爲您的建議是唯一的出路嗎? – pepe

+0

只是想知道,爲什麼不刷新行動迴應?我肯定會嘗試我的建議,看看它是否有效。我認爲您的數據下載可能會導致界面凍結,從而可能會破壞HUD。 – Ravi

回答

2

你嘗試把refreshPosts的整個代碼(而不僅僅是調用reloadData)調度塊內?我一定會試着看看它是否有效。我認爲您的數據下載可能會導致界面凍結,從而可能會破壞HUD。