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:
with no parameters
action:@selector(switchToNewsDetails)
with 1 parameter indicating the control that sends the message
action:@selector(switchToNewsDetails:)
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:
- set a tag property to each button equal to required index
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
}
How to add a function with parameter in addTarget of UIButton in swift 5?
I don't believe it is possible to pass custom parameters to a #selector
, as stated here,
Action methods must have a conventional signature. The UIKit framework permits some variation of signature, but both platforms accept action methods with a signature similar to the following:
meaning you method can at least have one of the following signatures:
@objc func buttonActionSkip(sender: UIButton)
@objc func buttonActionSkip(sender: UIButton, for event: UIEvent)
You can however, change the sender type. A possible solution for your problem could be extending the UIButton class adding a name
property:
class MyMainSkipButton: UIButton {
var name: String = ""
}
Then on your code:
//button initialization
let MainSkipButton = MyMainSkipButton(type: .custom)
MainSkipButton.frame = CGRect(x: 50 , y: 48, width: 47.0, height: 24.0)
MainSkipButton.backgroundColor = .blue
MainSkipButton.layer.cornerRadius = 10
MainSkipButton.layer.borderWidth = 1
MainSkipButton.layer.borderColor = UIColor.white.cgColor
MainSkipButton.setTitle(NSLocalizedString("Skip", comment: "Button"), for: .normal)
MainSkipButton.setTitleColor(.white, for: .normal)
MainSkipButton.name = "what ever the name is"
MainSkipButton.addTarget(self, action: #selector(buttonActionSkip(sender:)), for: .touchUpInside)
self.view.addSubview(MainSkipButton)
//function initialization
@objc func buttonActionSkip(sender: MyMainSkipButton) {
print("Receive parameter string is\(sender.name)")
sender.isHidden = true
}
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")
}
How to pass a Parameter to the addTarget method of button in swift
If the title to be passed is button's title, then you can simply do this :-
@objc func buttonPressed(sender: UIButton){
let title = sender.title(for: .normal)
}
And if it is some other data in section, you can use tags on your buttons, and setting them to indexPath.row :-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell.buttonView.tag = indexPath.row
}
And in your Button's action, access button's tag
@objc func buttonPressed(sender: UIButton){
let objectIndex = sender.tag
let object = yourArray[objectIndex]
let title = object.title
}
Related Topics
How to Convert String to Date Without Time in Swift 3
Swiftui Remove Transition of Fullscreen Cover
How to Pass a Completion Block to Another Class in Swift
How to Dynamically Hide Navigation Back Button in Swiftui
Swiftui Section from Attribute of a Struct
How to Change The Default Skscene That Displays on Startup
Why Does a Function Have Long-Term Write Access to All of Its In-Out Parameters
Comma Automatically Being Added to Textfield in Swift
Swift Applications Takes More Space on Disk
Using Cfarraygetvalueatindex in Swift with Unsafepointer (Aupreset)
Implementing Undo and Redo in a UItextview with Attributedtext
Avaudiopcmbuffer Built Programmatically, Not Playing Back in Stereo
Sound for Scene Transition, That Doesn't Stutter
Can't Set @Ibinspectable Computed Property in UIview
Swift Error "Static Member Cannot Be Used on Instance of Type"
Pass Custom Parameter to UIbutton #Selector Swift 3