Passing Parameters to Addtarget:Action:Forcontrolevents

Passing parameters to addTarget:action:forControlEvents

action:@selector(switchToNewsDetails:)

You do not pass parameters to switchToNewsDetails: method here. You just create a selector to make button able to call it when certain action occurs (touch up in your case). Controls can use 3 types of selectors to respond to actions, all of them have predefined meaning of their parameters:

  1. with no parameters

    action:@selector(switchToNewsDetails)
  2. with 1 parameter indicating the control that sends the message

    action:@selector(switchToNewsDetails:)
  3. With 2 parameters indicating the control that sends the message and the event that triggered the message:

    action:@selector(switchToNewsDetails:event:)

It is not clear what exactly you try to do, but considering you want to assign a specific details index to each button you can do the following:

  1. set a tag property to each button equal to required index
  2. in switchToNewsDetails: method you can obtain that index and open appropriate deatails:

    - (void)switchToNewsDetails:(UIButton*)sender{
    [self openDetails:sender.tag];
    // Or place opening logic right here
    }

Attach parameter to button.addTarget action in Swift

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
button.addTarget(self, action: "buttonClicked:",
forControlEvents: UIControlEvents.TouchUpInside)

Or for Swift 2.2 and greater:

button.tag = 5
button.addTarget(self,action:#selector(buttonClicked),
forControlEvents:.TouchUpInside)

Now do logic based on tag property

@objc func buttonClicked(sender:UIButton)
{
if(sender.tag == 5){

var abc = "argOne" //Do something for tag 5
}
print("hello")
}

Pass multiple parameters to addTarget

May be you can do something like this

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("CartCell", forIndexPath:indexPath) as! CartTableViewCell
cell.buyButton.tag = (indexPath.section*100)+indexPath.row
cell.buyButton.addTarget(self, action: "btnBuy_Click:", forControlEvents: .TouchUpInside)
}

func btnBuy_Click(sender: UIButton) {
//Perform actions here
let section = sender.tag / 100
let row = sender.tag % 100
let indexPath = NSIndexPath(forRow: row, inSection: section)
self.buyButton(indexPath, 2, 3 ,4 , 5, 6)
}

Create tag value according to you'r requirement and maintaint it's integrity too.

How can I pass a parameter in addTarget's action?

Just put a colon

Selector("textChange:")

When adding targets to UIControl you cannot have other parameters. Byy default Selectors in target will take the sender. So your selector should be

func textChange(sender : UITextfield)
{

}

But i sense you wanted to find which textfield is selected. So for that you might want to keep an identifier as tag value to UITextfield

ArrayofQuantityTextfield[i].tag = i;
ArrayofQuantityTextfield[i].addTarget(self, action: Selector("textChange:"), forControlEvents: UIControlEvents.EditingChanged)

In your Button Action

func textChange(sender : UITextfield)
{
if ( sender.tag == your_i_value )
{
//Do your stuff
}
}

How to pass multiple parameters addTarget?

If you want more then one perimeter pass then you can use a objc_setAssociatedObject.

Any thing will be pass like Dictionary,Array,String,Int.

import ObjectiveC

extension UIButton {
private struct AssociatedKeys {
static var WithValue = "KeyValue"
}

@IBInspectable var withValue: String? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.WithValue) as? String
}
set {
if let newValue = newValue {
objc_setAssociatedObject(
self,
&AssociatedKeys.WithValue,
newValue as NSString?,
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN
)
}
}
}
}

You need to use above extension:-

import ObjectiveC

button.tag = numbers[index];
button.addTarget(self, action: #selector(ViewController.buttonClicked(_:)), forControlEvents:UIControlEvents.TouchUpInside)

//set velue
button.withVelue = "1,2,3,4"

func buttonClicked(sender: UIButton){

print(sender.withVelue)
}

Passing parameters on button action:@selector

Edit. Found a neater way!

One argument that the button can receive is (id)sender. This means you can create a new button, inheriting from UIButton, that allows you to store the other intended arguments. Hopefully these two snippets illustrate what to do.

  myOwnbutton.argOne = someValue
[myOwnbutton addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];

and

- (IBAction) buttonTouchUpInside:(id)sender {
MyOwnButton *buttonClicked = (MyOwnButton *)sender;
//do as you please with buttonClicked.argOne
}

This was my original suggestion.

There is a way to achieve the same result, but it's not pretty. Suppose you want to add a parameter to your navigate method. The code below will not allow you to pass that parameter to navigate.

[button addTarget:self action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];

To get around this you can move the navigate method to a class of it's own and set the "parameters" as attributes of that class...

NavigationAid *navAid = [[NavigationAid alloc] init];
navAid.firstParam = someVariableOne
navAid.secondParam = someVariableTwo
[button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];

Of course you can keep the navigate method in the original class and have it called by navAid, as long as it knows where to find it.

NavigationAid *navAid = [[NavigationAid alloc] init];
navAid.whereToCallNavigate = self
navAid.firstParam = someVariableOne
navAid.secondParam = someVariableTwo
[button addTarget:navAid action:@selector(navigate) forControlEvents:UIControlEventTouchUpInside];

Like I said, it's not pretty, but it worked for me and I haven't found anyone suggesting any other working solution.

How do I pass a parameter in a action:@selector?

You can pass value to selector as below

[self performSelector:@selector(function:) withObject:@"myString"];

You can't pass a string to button action, iOS pass button itself to the method. If you want to use any string value there in method then you need to declare a variable or method which will return the particular string.

How to subclass an UIButton to pass multiple NSString parameters via addTarget:action:forControlEvents

The solution I found simply involves the definition of a class method visible to other classes:

+(void)myMethod:...

How to pass multiple values into @selector( ) for a UIButton?

You can't pass arbitrary parameters via target/action. The first parameter is sender, and the second (if you set it up this way) is the event. You could use the event to tell what kind of event triggered it, like so:

[btnRedPos addTarget:self action:@selector(setRedPos:forEvent:) 
forControlEvents:UIControlEventTouchDown];
[btnRedPos addTarget:self action:@selector(setRedPos:forEvent:)
forControlEvents:UIControlEventTouchUpInside];


- (void) setRedPos:(id)sender forEvent:(UIEvent*)event
{
UITouch* aTouch = [[event allTouches] anyObject];
if( aTouch.phase == UITouchPhaseBegan ) {
NSLog( @"touch began" );
}
else if( aTouch.phase == UITouchPhaseEnded ) {
NSLog( @"touch ended" );
}
}


Related Topics



Leave a reply



Submit