2014-01-19 100 views
0

我想用cocoapods使用SVProgressHUD。我已經在衆多的在線教程中將它安裝在我的xcode工作區中。它似乎很簡單,但我無法使用它。在這裏我的代碼從rottentomatoes應用程序加載數據。我想在網絡請求正在做它的事情時顯示「加載」。我想知道這是否是線程問題?我在網絡請求中添加了睡眠,因爲結果回來得太快!svprogresshud沒有顯示在xcode5中ios7

- (void) reload{ 
    [SVProgressHUD showWithStatus:@"Updating" maskType:SVProgressHUDMaskTypeBlack]; 

    NSString *url = @"http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?"; 

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
     [NSThread sleepForTimeInterval:3]; 
     NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
     NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[object objectForKey:@"movies"]]; 
     self.movies = [[NSMutableArray alloc]init]; 

     for(NSDictionary *item in array){ 
      SFMovie *movie = [[SFMovie alloc]initWithDictionary:item]; 
      [self.movies addObject:movie]; 

     } 
       [SVProgressHUD dismiss]; 
     [self.tableView reloadData]; 
    }]; 
} 

編輯點評: 重裝時呼籲兩國的init方法(因爲我是用故事板這個項目)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     [self reload]; 
    } 
    return self; 
} 
- (id) initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:aDecoder]; 
    if(self){ 
     [self reload]; 
    } 
    return self; 
} 

第二個編輯: 我添加了一個下拉刷新並且「更新...」出現在我拉下tableview的時候。但它不會在init上顯示。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    UIRefreshControl *refreshControl = [UIRefreshControl new]; 
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged]; 
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh..."]; 
    self.refreshControl = refreshControl; 
} 

- (void)refresh:(UIRefreshControl *)sender{ 
    [self reload]; 
    [sender endRefreshing]; 
} 

所以我想我不能在init方法上運行UI的東西,因爲視圖還沒有準備好。我現在在viewdidload上調用reload方法,它工作的文件。謝謝約翰!

+0

在主線程上調用'reload'嗎? – John

+0

@John reload是從init方法調用的,所以我想這是UI線程。謝謝。 –

+0

這不完全屬於答案,但我會建議將該邏輯移動到'viewDidLoad' - 因爲'tableView'在這一點上還沒準備好,用'[self.tableView reloadData]'調度請求回調引入了一個競爭條件。 – John

回答

0

在任何init方法中,混淆視圖層次結構都是棘手的業務 - 保留viewDidLoad中的邏輯以避免任何這些修改被擦除(特別是從故事板初始化)。

相關問題