0

I have added refreshcontrol to my table view , while pulling down , it should call a local method with dictionary as parameter. But i searched a lot but couldnt send a dictionary as a parameter in addTarget of uirefreshcontrol. Please help me calling a local method with dictionary as a parameter.

What i have tried is using objc and also without selector but it didnt work.

let defaultParam = ["buyer_id":UserDefaults.standard.string(forKey: "UserName")!,"transaction_type":""]
    refreshControl.addTarget(self, action: "loadStatement:", for: .valueChanged)
}
func loadStatement(parameter : Dictionary<String, Any>) {
}

It would be very helpful if i can call this local method with parameter i wish to send.

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Triambagan
  • 13
  • 3
  • https://stackoverflow.com/questions/24814646/attach-parameter-to-button-addtarget-action-in-swift ? It's old, but since it's using the same `addTarget()`, the logic remains the same. Same as in Objective-C too. – Larme Sep 10 '19 at 10:49
  • If the logic is same as it, please explain since i cant get it. – Triambagan Sep 10 '19 at 11:31

2 Answers2

0

The dictionary that you want to pass doesn't contain anything specific to the refreshControl.

You can create the defaultParams dictionary in the loadStatement() method itself instead of trying to pass it in the refreshControl event, i.e.

 func loadStatement() {
    var defaultParam = [String:Any]()
    defaultParam["buyer_id"] = UserDefaults.standard.string(forKey: "UserName")
    defaultParam["transaction_type"] = ""
    //rest of the code here...
 }

Or if there are some dependants, you can use instance properties instead.

Edit:

Try using default parameter values to get that working,

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...
 }
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • actually the method will be called in different places with different parameters. Thats why i am struggling. – Triambagan Sep 10 '19 at 11:31
  • Then try using instance property instead. You cannot pass that with refreshControl. – PGDev Sep 10 '19 at 11:33
  • Please explain by a example, couldnt get you. – Triambagan Sep 10 '19 at 11:38
  • You'll be adding target for the refreshControl only once. Why do you need different values for dictionary. Kindly clarify the context. – PGDev Sep 10 '19 at 11:40
  • What is the use of method1() ? – Triambagan Sep 10 '19 at 11:58
  • @Triambagan that's just an example of calling the method from multiple places like you specified. – PGDev Sep 10 '19 at 12:06
  • Actually, while calling the method loadStatement : ["abc":"xyz"] itselves i should pass the parameter or i will get nsinvalidargument exception. While calling in target i should pass the parameter but its not possible it seems. – Triambagan Sep 10 '19 at 12:17
0

I have found myself another kind of solution. Simply another method is written with no parameter and that is called. That empty method should call with the required parameter. And its fine now. Thanks for your concern.

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

    @objc func loadDatas(){
          loadStatement(parameter: ["buyer_id":UserDefaults.standard.string(forKey: "UserName")!,"transaction_type":""])
     }
Triambagan
  • 13
  • 3