如何在另一个 swift class 中结束刷新?

How can I end refreshing in another swift class?

你好,我有这个疑问,我可以在另一个 class 中结束刷新控制吗? 在我的 tableviewcontroller 中,我有这个:

func setRefreshGesture() {
let refreshGesture = UIRefreshControl()
refreshGesture.backgroundColor = UIColor.clearColor()
refreshGesture.tintColor = UIColor.whiteColor()
refreshGesture.addTarget(WorkUpdater.sharedInstance,action:#selector(WorkUpdater.attemptUpdateControl),forControlEvents:.ValueChanged)      
self.refreshControl = refreshGesture
self.refreshControl?.layer.zPosition = self.tableView.backgroundView!.layer.zPosition + 1
    }

在我的另一个 class.. 有这个:

 func attemptUpdateControl(){
        if !isUpdating && canPerformUpdate() {
            isUpdating = true
            performUpdateControl()
        } else {
            print("ERROR: - Update in progress")
        }
    }

 func performUpdateControl(){
        let reach = Reachability.reachabilityForInternetConnection()!
        if reach.isReachable() {
        work.getData()
        } else {
            UIAlertView(title: "Device without connection", message: "You must have an internet connection to use this feature", delegate: nil, cancelButtonTitle: "OK").show()

//这里我想结束对tableviewcontroller的刷新

}

}

您可以设置自定义 Delegate 或使用 NSNotifcationCenter。我不确定你的 TableViewControllerWorkUpdater 是什么关系。

因此,我将向您展示一种使用 NSNotifactionCenter 进行此操作的可能方法。在你的 TableViewController 里,把这个放在 viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.refreshCompleted), name:"refreshCompleted", object: nil)

然后有一个refreshCompleted func:

func refreshCompleted() {
    refreshGesture.endRefreshing()      //you have to set it as a class var
}

同时更新 WorkUpdater 为:

func performUpdateControl(){
    let reach = Reachability.reachabilityForInternetConnection()!
    if reach.isReachable() {
    work.getData()
    } else {
        UIAlertView(title: "Device without connection", message: "You must have an internet connection to use this feature", delegate: nil, cancelButtonTitle: "OK").show()
    }
    //add this        
    NSNotificationCenter.defaultCenter().postNotificationName("refreshCompleted", object: nil)
}