Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh control target action not triggered

My viewcontroller has a tableview, in which i populate my views in different sections. I am trying to add a refresh control to this tableview. What I have implemented is as below.

func setUpRefreshControl() {
    let refreshControl = UIRefreshControl()
    refreshControl.tintColor = UIColor.red
    refreshControl.addTarget(self, action: #selector(handleRefresh(_: )), for: UIControlEvents.valueChanged)
    if #available(iOS 10.0, *) {
        tableView.refreshControl = refreshControl
    } else {
        tableView.addSubview(refreshControl)
    }
}


@objc func handleRefresh(_ sender: UIRefreshControl) {
   print("-----------REFRESHED------------")
}

Next, when I pull down the tableview to refresh, the refresh control (red in color as setup)is visible. However, the target function is not called.

My viewcontroller is inside a tab bar controller, which is embedded in a navigation controller. I guess the issue is related to view hierarchy because when I try the same code in a separate project with no nav bar and tab bar, its working fine. But I cannot figure out what the current issue is. Any suggestion on solving this? Thank you.

EDIT: I tested it in different simulators: 6, 6s, 6s+, 7, 7+, 8, 8+, X. I found that the above code runs fine in all the plus versions including X. However, all the simulators are running ios 11.2 so I still cannot figure it out what might be causing this issue.

like image 962
Sujal Avatar asked Apr 09 '18 04:04

Sujal


3 Answers

I was having a similar issue, pull to refresh would work fine on 5.5" screens but all other phones would show only a partial animation then never quite trigger the refresh.

I found invoking refreshControl.didMoveToSuperview() in the viewDidAppear for my view controller fixed the problem.

If I understand correctly, this same method is invoked when you set tableView.refreshControl = refreshControl but needs recalculation by the time the view is actually set to appear. I'll need to dig in a bit more to be sure of the specifics, but hope this is helpful for the time being. 🙂

like image 132
thetonewolf Avatar answered Oct 16 '22 10:10

thetonewolf


I solved this issue changing the property Size in Simulated Metrics to Inferred and the property Simulated Size to Fixed in the view controller.

I hope this works for you.

like image 7
Elison Coelho Avatar answered Oct 16 '22 09:10

Elison Coelho


This is the refresh control code which i implemented and its working for me in my current project, just 3 days ago.

var refreshControl: UIRefreshControl!

this is my global refreshControl variable and below is the implementation of it.

    //MARK: - REFRESH CONTROLLER VIEW
    refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(refresh(_ :)), for: UIControlEvents.valueChanged)
    refreshControl.attributedTitle = NSAttributedString(string: "")
    tblRequest.addSubview(refreshControl)

Hope, this works for you ! :-)

like image 1
Zahurafzal Mirza Avatar answered Oct 16 '22 09:10

Zahurafzal Mirza