繁体   English   中英

我可以在 swift 中为 UIViewController 而不是 UITableViewController 创建一个下拉刷新吗?

[英]Can I create a pull to refresh for UIViewController instead of UITableViewController in swift?

我有一个简单的应用程序,不需要用 UITableViewController 构建。 有没有机会为简单的 UIViewController 创建“拉动刷新”功能?

是的,有。 事实上,为了使用UIRefreshControl只有一个要求 - 您可以将UIRefreshControl放在UIScrollView或其子类(例如UITableViewUICollectionView )中。

Swift 中的示例代码:

override func viewDidLoad() {
{
    super.viewDidLoad()
    //...
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(didPullToRefresh), forControlEvents: .ValueChanged)

    scrollView.addSubview(refreshControl)
}

func didPullToRefresh() {

}

Objective-c 中的示例代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //...

    UIRefreshControl *refreshControl = [UIRefreshControl new];
    [refreshControl addTarget:self action:@selector(didPullToRefresh) forControlEvents:UIControlEventValueChanged];

    [self.scrollView addSubview:refreshControl];
 }

 - (void)didPullToRefresh
 {

 }

从 iOS 6 refreshControlUIScrollView有一个refreshControl属性(因此它是子类UITableViewUICollectionView )。

Apple 文档中的代码: https : //developer.apple.com/documentation/uikit/uirefreshcontrol#//apple_ref/occ/instm/UIRefreshControl

func configureRefreshControl () {
   // Add the refresh control to your UIScrollView object.
   myScrollingView.refreshControl = UIRefreshControl()
   myScrollingView.refreshControl?.addTarget(self, action:
                                      #selector(handleRefreshControl),
                                      for: .valueChanged)
}

@objc func handleRefreshControl() {
   // Update your content…

   // Dismiss the refresh control.
   DispatchQueue.main.async {
      self.myScrollingView.refreshControl?.endRefreshing()
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM