【发布时间】:2013-12-18 15:04:12
【问题描述】:
我正在构建一个需要能够通过拉取刷新的应用程序,然后重新运行 ViewDidLoad。 所以我知道我可以使用 UIRefreshControl,但我只能找到在 UITableViewController 中使用的代码。 有没有人知道如何在 UIView 中而不是在 UITableView 中使用 UIRefreshControl。 提前致谢。
【问题讨论】:
标签: xcode uiview uirefreshcontrol
我正在构建一个需要能够通过拉取刷新的应用程序,然后重新运行 ViewDidLoad。 所以我知道我可以使用 UIRefreshControl,但我只能找到在 UITableViewController 中使用的代码。 有没有人知道如何在 UIView 中而不是在 UITableView 中使用 UIRefreshControl。 提前致谢。
【问题讨论】:
标签: xcode uiview uirefreshcontrol
试试这个……
[myView setNeedsDisplay];
这将重新加载您的视图控制器。你可以把它放在一个方法中,并在拉动刷新期间调用它……希望这对你有帮助。 快乐编码;)
【讨论】:
很容易: 将控件保留为成员或属性:
UIRefreshControl *_refreshControl;
将此添加到您的 viewDidLoad 方法中:(只需将控件添加到您的 tableView。确保 tableView 不是 nil of course)。
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[_tableView insertSubview:_refreshControl atIndex:0];
实现刷新方法:
- (void)refresh:(UIRefreshControl *)refreshControl {
[self reloadData];
//Don't forget to stop the refreshing animation after data reloads.
[_refreshControl endRefreshing];
}
【讨论】:
UIRefreshControl 不能在没有表格的情况下使用。 the documentation 中的这条注释特别警告不要这样使用:
注意: 因为刷新控件是专门为在由表视图控制器管理的表视图中使用而设计的,所以在 不同的上下文可能会导致未定义的行为。
因此,尽管可以使控件工作,但您确实不应该在没有表格的情况下使用它。滚动您自己的刷新控件是更好的解决方案。
更好的是,尝试设计您的应用,使用户无需刷新视图。应用程序应该知道何时有新数据可用。让用户刷新的唯一借口是,如果自动这样做会以某种方式使用户感到困惑或使应用程序更难使用。
【讨论】: