문제

In our iPgone & iPad app we use push segue transitions between different ui contollers, most of them extend UICollectionViewController. In each controller we load data from our internal API. Loading is done viewWillAppear or viewDidLoad.
Now, the thing is, that this API call sometime can take a second or two, or even three... well, lot's of stuff there, let's assume we can't change it. But, we can change the user experience and at least add the "loading" circle indicator. The thing is, what I can't understand by means of correct concept, while transition from A to B, the "load" is done at B, while page A still presented. So, question is "how do I show indicator on page A, while loading controller for page B?"

Thanks all,
Uri.

도움이 되었습니까?

해결책

Common approach in this case is to load data in destination view controller NOT in main thread. You can show indicator while loading data in background thread and then remove it.

Here is sample of code from my project solving the same problem:

- (void) viewDidLoad {
    ...
    // add indicator
    self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.spinner.hidesWhenStopped = YES;
    self.spinner.center = self.view.center;
    [self.view addSubview:self.spinner];

    ...    
    // fetch news
    [self.spinner startAnimating];
     __weak typeof(self) weakSelf = self
    [[BitrixApiClient sharedInstance] getLatestNewsWithCompletionBlock:^(NSArray *newsArray, NSUInteger maxPageCount, NSUInteger currentPageNumber, NSError *error) {
        if (!error) {
            weakSelf.newsArray = newsArray;
            weakSelf.currentPageNumber = currentPageNumber;

            [weakSelf.newsTableView reloadData];
        }
        // stop spinning
        [weakSelf.spinner stopAnimating];
    }];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top