【问题标题】:How to check if the refresh control is or isn't refreshing inside its target action method如何检查刷新控件是否在其目标操作方法中刷新
【发布时间】:2019-09-07 06:41:10
【问题描述】:

我有一个collectionView 和一个refreshControl

lazy var refreshControl: UIRefreshControl = {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self,
                             action: #selector(handleRefreshControl(_:)),
                             for: .valueChanged)
    return refreshControl
}()

//create collectionView
collectionView.refreshControl = refreshControl

我按下一个按钮从服务器获取一些数据。一旦进程开始,我使用refreshControl.refreshManually() 开始刷新。

getDataBarButton() {

    refreshControl.refreshManually()

    fetchData()
}

一旦检索到数据,我就会调用refreshControl.endRefreshing() 来停止刷新。

func fetchData() {

    // got the data

    refreshControl.endRefreshing()
}

所有这些都可以正常工作。这不是从数据库中提取更多数据的情况,因为只有按钮可以这样做。一旦我拉出简历,我想显示refreshControl,然后将其删除。我不想在完成后完全摆脱refreshControl,因为按钮可能会再次被按下。

问题是,一旦我拉出简历,refreshControl 就会开始刷新并且不会停止。它只是停留在那里。

我检查它是否停止了,但这不起作用:

@objc fileprivate func handleRefreshControl(_ sender: UIRefreshControl) {
    if refreshControl.isRefreshing {
        print("refreshing is occurring")
        return
    }
    refreshControl.refreshManually()
    refreshControl.endRefreshing()
}

在我看来,在返回数据并调用 after refreshControl.endRefreshing() 之后,一旦我将 cv 拉到 if refreshControl.isRefreshing 内的 print 语句 不应该 运行但它确实

在创建和切换变量(如var isRefreshing = false/true)之外,如何检查refreshControl 是否在其target action 内刷新?

只是为了上下文,如果您正在阅读有关堆栈溢出 iOS 应用程序的这个问题,只需下拉并观察会发生什么。没有什么可刷新的,但是当拉动发生时,会显示一个刷新控件,然后将其删除。我想做同样的事情。

【问题讨论】:

  • 这个refreshControl.endRefreshing() 应该在请求的完成处理程序中。
  • 它确实在请求的完成处理程序中被调用。它发生在 firebase 回调中,我在 mainQueue 上调用它。我没有添加代码,因为我认为它与问题无关。不过谢谢

标签: ios swift uicollectionview uirefreshcontrol


【解决方案1】:

除了使用属性我找不到其他方法

var isRefreshing = false // 1. initially set to false

func startRefreshing() {

    isRefreshing = true // 2. create a function that sets isRefreshing to true and also calls .refreshManually
    refreshControl.refreshManually()
}

func endRefreshing() {
    isRefreshing = false  //  3. create a function that sets isRefreshing to false and also calls .endRefreshing
    refreshControl.endRefreshing()
}

getDataBarButton() {

    startRefreshing() // 4. when then button is pressed call the function to start refreshing
    fetchData()
}

func fetchData() {

    // got the data
    endRefreshing()  // 5. after the data is returned call the function to endRefreshing
}

func handleRefreshControl(_ sender: UIRefreshControl) {

    // 6. in the refresher's target action check to see if isRefreshing == false and if it is then call refreshControl.endRefreshing()
    if !isRefreshing {

        refreshControl.endRefreshing()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多