Pass Multiple Parameters to Addtarget

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 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 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")
}

Adding multiple arguments to a target #selector in a UITableView

You can´t pass parameters to your selector. Since you´re creating a button, why not just set the buttons tag to the id of the user and then in your followButtonTapped, just access it with sender.tag.

So:

var fbFriends = [People]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: FBFriendsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "fbFriendsCell", for: indexPath) as! FBFriendsTableViewCell

cell.friendFollowButton.tag = fbFriends[indexPath.row].userId
cell.friendFollowButton.addTarget(self, action: #selector(followButtonTapped(_:)), for: .touchUpInside)

return cell
}

@objc func followButtonTapped(_ sender: UIButton) {

user.followUser(token: Helper.shared.getAccessToken()!, userId: Helper.shared.retriveUserID()!, followeeId: sender.tag) { (status, code, err, msg, body) in

//Do something with response
}
}

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