I want to pass the selector for a function to another function. Right now I'm just passing the string and doing several if statements. If I could pass the actual function selector to the function, I could eliminate all the if statements.
My current function:
func getBtn(i: String, f: String) -> UIBarButtonItem
{
let btn: UIButton = UIButton();
btn.frame=CGRectMake(0,0,30,30)
btn.setBackgroundImage(UIImage(named:i), forState: UIControlState.Normal)
if f == "funcOne"{ btn.addTarget(self, action: #selector(self.funcOne), forControlEvents:.TouchUpInside) }
else if f == "funcTwo"{ btn.addTarget(self, action: #selector(self.funcTwo), forControlEvents:.TouchUpInside) }
else if f == "funcThree"{ btn.addTarget(self, action: #selector(self.funcThree), forControlEvents:.TouchUpInside) }
else if f == "funcFour"{ btn.addTarget(self, action: #selector(self.funcFour), forControlEvents:.TouchUpInside) }
// etc.
let barBtn = UIBarButtonItem(customView: btn)
return barBtn;
}
Usage:
items.append(self.getBtn("checkmark",f:"funcOne"));
What I'd like to be able to do:
func getBtn(i: String, f: SOME_TYPE_OF_THING) -> UIBarButtonItem
{
let btn: UIButton = UIButton();
btn.frame=CGRectMake(0,0,30,30)
btn.setBackgroundImage(UIImage(named:i), forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(f), forControlEvents:.TouchUpInside)
let barBtn = UIBarButtonItem(customView: btn)
return barBtn;
}
Usage:
items.append(self.getBtn("checkmark",f:self.funcOne));
To pass function as parameter to another function in Swift, declare the parameter to receive a function with specific parameters and return type. The syntax to declare the parameter that can accept a function is same as that of declaring a variable to store a function.
The solution to your problem is to pass the object that should run the selector method along with the selector to the initialisation of the ValueAnimator object. Also update the timerCallback() : @objc func timerCallback() { ... _ = target.
The purpose of #selector()
syntax is to save you from the possible error in having to write a selector as a literal string, which is error-prone. Nevertheless, you are free to write Selector(f)
instead of using #selector()
syntax.
However, it would be even better if f
were itself typed as Selector:
func getBtn(i: String, f: Selector) -> UIBarButtonItem
That way, you could keep using #selector
but do it in the call:
items.append(self.getBtn("checkmark",f:#selector(funcOne)))
In getBtn
you would then just pass f
directly as the button action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With