UIRefreshControl freezes when temporarily showing other tab (iOS 7)

此生再无相见时 提交于 2019-12-10 16:37:31

问题


I have the following on one of my tabs:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = YES;

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

    //### Workaround: http://stackoverflow.com/a/19126113/1971013
    dispatch_async(dispatch_get_main_queue(), ^
    {
        [self.refreshControl beginRefreshing];
        [self.refreshControl endRefreshing];
    });
}

- (void)refresh:(id)sender
{
    if ([Settings sharedSettings].haveAccount == YES)
    {
        [[DataManager sharedManager] synchronizeWithServer:^(NSError* error)
        {
            [sender endRefreshing];
        }];
    }
    else
    {
        [sender endRefreshing];
    }
}

Refresh control starts spinning normally when pulling down table.

However, when I, while it spins, shown an other tab shortly and then go back, the refresh control stops spinning.

Any idea why?


回答1:


Try moving this piece of code from viewDidLoad to viewWillAppear:

//### Workaround: http://stackoverflow.com/a/19126113/1971013
dispatch_async(dispatch_get_main_queue(), ^
{
    [self.refreshControl beginRefreshing];
    [self.refreshControl endRefreshing];
});



回答2:


The more sensible approach to fix this issue would be to end / begin refreshing on viewWillDisappear / viewWillAppear respectively

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    refreshControl.endRefreshing()
}

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)

    if isLoadingData {
        // for simplicity using harcoded height for refresh control
        tableView.setContentOffset(CGPoint(x: 0, y: -60), animated: false)
        refreshControl.beginRefreshing()
    }
}


来源:https://stackoverflow.com/questions/22622530/uirefreshcontrol-freezes-when-temporarily-showing-other-tab-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!