我已将刷新控件添加到 TableView 中,在下拉时,它应该调用以字典作为参数的本地方法。但我搜索了很多,但无法在 uirefreshcontrol 的 addTarget 中发送字典作为参数。请帮助我调用以字典作为参数的本地方法。
我尝试过使用 objc 并且没有选择器,但它不起作用。
let defaultParam = ["buyer_id":UserDefaults.standard.string(forKey: "UserName")!,"transaction_type":""]
refreshControl.addTarget(self, action: "loadStatement:", for: .valueChanged)
}
func loadStatement(parameter : Dictionary<String, Any>) {
}
如果我可以使用我希望发送的参数调用这个本地方法,那将非常有帮助。
最佳答案
您要传递的字典
不包含任何特定于refreshControl
的内容。
您可以在 loadStatement()
方法本身中创建 defaultParams
dictionary
,而不是尝试将其传递到 refreshControl
事件,即
func loadStatement() {
var defaultParam = [String:Any]()
defaultParam["buyer_id"] = UserDefaults.standard.string(forKey: "UserName")
defaultParam["transaction_type"] = ""
//rest of the code here...
}
或者,如果有一些依赖项,您可以改用实例属性。
编辑:
尝试使用默认参数值来使其正常工作,
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(loadStatement(parameter:)), for: .valueChanged)
}
func method1() {
var params = [String:Any]()
//....
loadStatement(parameter: params) //called with custom params here....
}
@objc func loadStatement(parameter: [String:Any] = ["buyer_id":UserDefaults.standard.string(forKey: "UserName")!,"transaction_type":""]) {
print(parameter)
//rest of the code here...
}
关于swift - 如何将静态参数传递给 addTarget #Selector 中 UIRefreshControl() 的本地函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57868678/