如何根据 segmentedControl 的选定索引更改刷新器的功能

How do I change the function of a refresher based on the selected index of a segmentedControl

我有一个 vc 和一个 segmentedControl,我希望每次 segmentedControl 的索引改变时都能改变刷新器调用的函数

lazy var refresher: UIRefreshControl = {
    let refreshControl = UIRefreshControl()
    if (segmentedControl.selectedSegmentIndex == 0){
        refreshControl.addTarget(self, action: #selector(loadRecommend1), for: .valueChanged)
    } else if (segmentedControl.selectedSegmentIndex == 1){
        refreshControl.addTarget(self, action: #selector(loadCompletePosts1), for: .valueChanged)
    } else if (segmentedControl.selectedSegmentIndex == 2){
        refreshControl.addTarget(self, action: #selector(loadCompletePostsByHealthy1), for: .valueChanged)
    }
    return refreshControl
}()

目前,尽管在 segmentedControl 中选择了项目,但每次都会调用第一个函数

lazy var 仅在创建此实例时调用一次,而不是在段索引更改时调用,因此您要么在 1 个选择器方法中更改该目标选择器,要么只有 1 个选择器执行方法当前段索引如

refreshControl.addTarget(self, action: #selector(allCalls), for: .valueChanged)

@objc func allCalls(_ sender:UIRefreshControl) {
   if (segmentedControl.selectedSegmentIndex == 0){
        loadRecommend1()
   } else if (segmentedControl.selectedSegmentIndex == 1){
        loadCompletePosts1() 
   } else if (segmentedControl.selectedSegmentIndex == 2){
        loadCompletePostsByHealthy1() 
   }
}