2014-01-16 69 views
0

在此項目中,我從服務器獲取JSON內容時使用了MBProgressHUD。 我第一次啓動我的應用程序時,從我的MenuViewController到我的頁面的動畫工作正常。但是當我回到我的Menu和頁面時,沒有顯示viewcontroller切換動畫。 當我添加了MBProgressHUD功能時纔會發生..MBProgressHUD UIViewController動畫不一致

我的頁面viewDidLoad方法:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    [self performSelectorInBackground:@selector(loadPage) withObject:nil]; 
} 

的loadPage方法,簡化:

-(void) loadPage { 
    //loading functionality, deleted for now. 


    dispatch_async(dispatch_get_main_queue(), ^{ 

     [_lblTitle setText: [postsArray[0] objectForKey:@"title"]]; 
     [_webview loadHTMLString:[postsArray[0] objectForKey:@"content"] baseURL:nil]; 
     [HUD hide:YES]; 
    }); 

} 

所以,當我刪除第二和3rth線在viewDidLoad和HUD hide中,動畫每次都能正常工作。 我在做什麼錯?

回答

2

更換

HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    [self performSelectorInBackground:@selector(loadPage) withObject:nil]; 

MBProgressHUD *searchHUD = [[MBProgressHUD alloc] initWithView:self.view]; 
     searchHUD.dimBackground=YES; 
     searchHUD.animationType = MBProgressHUDAnimationZoom; 
     [self.view addSubview:searchHUD]; 
     [searchHUD showWhileExecuting:@selector(loadPage) onTarget:self withObject:Nil animated:YES]; 

原因:MBProgressHud提供此功能在後臺運行的線程的任何方法..

+0

這似乎工作!我之前嘗試過,但是由於某種原因我的標題標籤沒有填充,後退按鈕(未在我的示例代碼中顯示)也未顯示。但現在它可以工作。謝謝! – harmjanr

+0

總是歡迎:) – Ashutosh

2

-(void)viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
    [self.view addSubview:HUD]; 
    HUD.delegate = self; 

    [HUD show:YES]; 

    [self performSelectorInBackground:@selector(loadPage) withObject:nil]; 
} 
試試這個
+0

我試過(並添加MBProgressHUDDelegate到我的.h文件),但仍然沒有動畫.. – harmjanr

0
//MBProgressHUD provides an easy way to execute code in background you don't need to create separate method for each task you wish to execute with HUD 

MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view]; 
[self.view addSubview:hud]; 

[hud showAnimated:YES whileExecutingBlock:^{ 
    //Write the code you wish to execute with an indicator 
    // e.g. write the code to call web-service 

// Last but not the least if you are doing any UI related task then use 

    // Write UI related code to be executed on main queue 
//e.g. if you are using UITableView to show retrieved data from server 
dispatch_async(dispatch_get_main_queue(), ^{ 
[self.tableView reloadData]; 

}); 


} completionBlock:^{ 
    [hud removeFromSuperview]; 
}];