您好,在我的应用程序中,我想将拉动刷新 Controller 添加到 ScrollView 。 我编写了以下代码,但没有得到任何响应。
每当我 ScrollView 时,都不会调用添加目标方法。
@IBOutlet var scrool: UIScrollView!
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
// Add refresh control for the Home page scroll view.
self.refreshControl.tintColor = UIColor.appcolor
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: UIControl.Event.valueChanged)
self.scrool.isScrollEnabled = true
self.scrool.alwaysBounceVertical = true
scrool.addSubview(refreshControl)
}
@objc func refresh(sender:AnyObject) {
// Code to refresh table view
self.getHomePageApi()
refreshControl.endRefreshing()
}
最佳答案
您好,当我在 viewwillappear() 中编写此代码时解决了这个问题,除了 viewdidload() 函数之外,它可以正常工作。
var refreshControl:RefreshControl!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
refreshControl = RefreshControl()
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
scrool.refreshControl = refreshControl
}
@objc func refresh()
{
// Code to refresh table view
refreshControl.endRefreshing()
self.getHomePageApi()
}
关于ios - 如何将拉动刷新 Controller 添加到 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730638/