Search code examples
swiftswift2reachabilityuirefreshcontrol

How can I end refreshing in another swift class?


Hello im having this doubt can i end a refresh control in another class? In my tableviewcontroller i have this:

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
    }

and in my other class.. have this:

 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()

//here i want to end my refresh of tableviewcontroller

}

}


Solution

  • You could either setup custom Delegate or use NSNotifcationCenter. I am not sure what is your TableViewController relationship with WorkUpdater.

    So I am showing you a possible way of doing it with NSNotifactionCenter. In your TableViewController, have this in viewDidLoad

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

    Then have a refreshCompleted func:

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

    Also update the WorkUpdater with this:

    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)
    }