2012年8月28日 星期二

MBProgressHUD 方便建造thread, 並且不檔到navigation item(比如back)

http://github.com/matej/MBProgressHUD

.h檔部分
#import "MBProgressHUD.h"  
.....{
     
    MBProgressHUD *HUD;
}

.m檔部分
- (voidviewDidLoad{
    
    [super viewDidLoad];

    HUD = [[MBProgressHUD alloc] initWithView:self.tv];
    // 這裡是navigation的item如back在有loading image時可以用的關鍵,記住initWithView不可以指定  self.view,因為這樣以來loading會覆蓋過整個view,關鍵是要另外指定一個較小不是全範圍的view如table view來init.
    [self.tv addSubview:HUD]; 
    // 這裡則是要指定那個較小的view去做addSubview:HUD,如此以來執行的時候也可以按navigation的item了

    HUD.delegate = self;  
    HUD.labelText = @"Loading";  
    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}

- (void) myTask {  
     // Do something usefull in here instead of sleeping ...
     // 背景運作的code如網路載入資料等放這...
}

- (void)hudWasHidden { 
    // Remove HUD from screen when the HUD was hidded  
    [HUD removeFromSuperview];
}

沒有留言: