How to send additional parameter in addtarget function?

how to pass parameter in selector ios
pass parameter to action swift
swift 4 selector with multiple arguments
uibutton addtarget selector with arguments swift
uibutton addtarget with parameters swift 3
objective-c action:@selector with parameters
swift button parameter
addtarget swift 4

please anyone tell me, How to send additional parameter in addtarget function in swift 2.2? Example:

button.addTarget(self, action: #selector(classname.myFunc(_:)), forControlEvents: UIControlEvents.TouchUpInside))

func myFunc(sender:UIButton){
    print("i am here")


func myFunc(sender:UIButton,parameter:String){
    print("i am here")

You can create a subclass of UIButton, add one property named parameter which you want to pass as an extra parameter.

In addTarget(), you can only choose passing the caller or not. In this case, the caller is a UIButton.

So use the subclass and call addTarget(), then you can get the string which you want send:

func myFunc(sender: customUIButton) {
    // do something with sender.parameter

Hope it can help you.

Attach parameter to button.addTarget action in Swift, If you want to send additional parameters to the buttonClicked method, for example an indexPath or urlString, you can subclass the UIButton: class� To understand how to pass a value as a parameter through an onClick event handler, take a look at the line of code inside of the return statement. It’s a single button with one event handler: onClick. Typically, to call a function when we click a button in React, we would simply pass in the name of the function to the onClick handler, like so:

In Swift 2.2 you can do something like this.

myUISlider.addTarget(self, action:#selector(self.onSliderValChanged(_:withEvent:)), forControlEvents: .ValueChanged)

func onSliderValChanged(slider: UISlider, withEvent event: UIEvent?) {

Help with passing multiple parameters through .addTarget to , //I don't think I can use tags because I need to send more variables and not use Int's } you could wrap your loop in another function to be called by the selector. The control does not retain the object in the target parameter. It is your responsibility to maintain a strong reference to the target object while it is attached to a control. Specifying a value of 0 for the control Events parameter does not prevent events from being sent to a previously registered target and action method.

I use sender properties to send some parameters with it like

button.accessibilityHint = "param 1"
button.accessibilityValue = "param 2"
button.tag = 0

button.addTarget(self, action: #selector(buttonPressed(_:)),   forControlEvents: UIControlEvents.TouchUpInside))

func myFunc(sender:UIButton){

 print(sender.accessibilityHint) // param1
 print(sender.accessibilityValue) // param2
 print(sender.tag) // 0


pass parameter extra parameter button click ios swift target action , You cannot pass custom parameters in addTarget: .One alternative is set the tag property of button and do work based on the tag. button.tag = 5� So, in this post, I am going to tell you how to create a Python function with optional arguments. Creating a function with optional arguments in Python is quite an easy task. Let’s see with our first exam. Below we have created a Python function with a default argument: def my_function(a=10): print(a) my_function()

It's not possible. You have to use a workaround.

button.addTarget(self, action: #selector(classname.myFunc(_:)), forControlEvents: UIControlEvents.TouchUpInside))

func myFunc(sender:UIButton){
    self.myFunc2(sender, parameter: "My parameter")

func myFunc2(sender:UIButton,parameter:String){

Attach parameter to button.addTarget action in Swift, swift button action function argument of '#selector' does not refer to an '@objc' method, property, or initializer uibutton add target swift 4 swift pass selector as� Namespace.ServiceName.Method(parameters, successCallback, failedCallback, userContext) The userContext parameter is a value that you can pass from the calling method to the success and failed callback methods.Your successful callback method will look like this. function serviceCallback(result, context, methodName) { }

Swift 5.0 code

theButton.addTarget(self, action: #selector(theFunc), for: .touchUpInside) = "myParameter"


@objc func theFunc(sender:UIButton){ 

Pass multiple parameters to addTarget, And I want to add action to it by passing multiple parameters in cellForRowAtIndexPath method. func tableView(tableView: UITableView, cellForRowAtIndexPath� 2. Define View Function In Django App File. Now let us define the emp_detail function in DjangoHelloWorld / dept_emp / file. ''' This function will retrieve employee detail information and return the emp_detail.html page to client. This function contains two url parameter user_name and mobile_number.

addTarget(_:action:for:), Associates a target object and action method with the control. Specifying a value of 0 for the controlEvents parameter does not prevent events from being sent� The send function sends data on a connected socket. Syntax int WSAAPI send( SOCKET s, const char *buf, int len, int flags ); Parameters. s. A descriptor identifying a connected socket. buf. A pointer to a buffer containing the data to be transmitted. len. The length, in bytes, of the data in buffer pointed to by the buf parameter. flags

Target-Action Explained In Swift – LearnAppMaking, Target-Action is a mechanism that's used to call a certain function on a We're calling the function addTarget(_:action:for:) on button with 3 parameters: You can pass it around in your code, just like any other value or� Swift: how to add params in button.addTarget [duplicate] This question already has an answer here: Attach parameter to button.addTarget action in Swift 11 answers I have a func below, but can`t send href for func.

Pass arguments to a Selector with Swift, In this case, we would create a subclass of UIButton , we would then be able to add variables and functions to it to do and store whatever we want� Function computeCommission(ByVal commRate As Double, ByVal Sales As Currency) As Currency 'Passing an argument by value in a procedure using the ByVal keyword 'The declaration of the computeCommission function contains two variables as arguments, with Currency as the return data type. computeCommission = commRate * Sales. End Function

  • Possible duplicate of Attach parameter to button.addTarget action in Swift
  • This wont solve the issue unless the parameter is a global varibale. If that was the case then we didnt need to pass the parameter itself. Selector can only provide the caller as sender. Nothing more.
  • @kandelvijaya What issue are you referring to? OP didn't mention any. Anyway, it's possible OP wants to call myFunc2 with string X when button1 is tapped, and myFunc2 with string Y when button2 is tapped. So there is nothing wrong with this design.