我正在使用以下代码创建UIRefreshControl:
- (void) viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(doLoad) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;
}
- (void) doLoad
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// Instead of sleeping, I do a webrequest here.
[NSThread sleepForTimeInterval: 5];
dispatch_async(dispatch_get_main_queue(), ^{
[tableView reloadData];
[self.refreshControl endRefreshing];
});
});
}
它工作得很好。如果我导航到我的视图,拖动表格,代码运行并显示数据。
然而,我想要做的是在视图出现时将其设置为“加载”状态(这样用户就知道正在发生什么)。我尝试添加以下内容:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.refreshControl beginRefreshing];
}
但似乎不起作用。当我导航到视图时,它看起来像是常规视图(刷新控件不可见),而且当我尝试拉动刷新控件时,它从未完成加载。
显然,我做错了。您对我该如何处理有什么建议吗?