addTarget method is used to assign actions to UIControl elements in IOS.
Questions tagged [addtarget]
92 questions
71
votes
19 answers
Adding a closure as target to a UIButton
I have a generic control class which needs to set the completion of the button depending on the view controller.Due to that setLeftButtonActionWithClosure function needs to take as parameter a closure which should be set as action to an unbutton.How…

Ilker Baltaci
- 11,644
- 6
- 63
- 79
30
votes
7 answers
Press-and-hold button for "repeat fire"
I have referred to countless other questions about a press-and-hold button but there aren't many related to Swift. I have one function connected to a button using the touchUpInside event:
@IBAction func singleFire(sender: AnyObject){
…

Tim
- 583
- 1
- 6
- 18
13
votes
2 answers
Programming a UIButton is not working in Swift 4 Xcode 9 Beta 6, Cannot add target
In the past I had no problem creating a UIButton programmatically, but ever since I've been using Xcode 9 and Swift 4, I cannot find a way to make this error go away.
//Adding target to UIButton
func PositionStartButton(xOffset: Float){
…

Olivier
- 145
- 1
- 1
- 8
11
votes
2 answers
How to 'addTarget' to UILabel in Swift
I am trying to add labels in Swift, which are added in a loop. I then want to add a 'TapGesture' event to each one as they are added. It works , but the problem is that the function called takes data from the label to use when it's clicked, but the…

gareth power
- 209
- 2
- 4
- 12
7
votes
5 answers
Difference between UITapGestureRecognizer and addTarget
Can anyone clarify the difference between these 2 ways of triggering a function when tapping a view?
1)
myView.addTarget(self, action: #selector(myFunctionToTrigger(_:)), forControlEvents: UIControlEvents.TouchUpInside)
2)
let tapGesture =…

Daniele B
- 19,801
- 29
- 115
- 173
5
votes
1 answer
How does UIButton addTarget self work?
I try figure out why self point to the GameViewController instead of Answer
GameViewController.swift
class GameViewController: UIViewController {
var gameplay = QuestionsController(colors: colors)
override func viewDidLoad() {
…

Atsuo Sakakihara
- 179
- 1
- 3
- 12
4
votes
2 answers
How to add target to keyboard done/go/search buttons?
I want to attach a action to button, which is on keyboard. I don't want to create a custom keyboard, only handle search/go/done buttons actions.
Thanks!

gkhanacer
- 575
- 7
- 25
4
votes
2 answers
ios Create new file and add to all target automatically
Currently, I have 2 target like this.
When I create some file, it only choose 1 for default like this. I have to choose all target manually.
I afraid that developers might make some mistakes and don't add to app store version. Is there any way to…

Khant Thu Linn
- 5,905
- 7
- 52
- 120
4
votes
1 answer
addTarget not working for UIButton created in Swift
So, I've created a UIButton using Swift, and I animate it and it's superview using the following code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
…

Tony Armstrong
- 638
- 7
- 17
3
votes
1 answer
Swift addTarget to a button and call function in different View Controller file from UIview file
now I practice applying the MVC pattern in my Swift project.
I have one View Controller (VC) file and one UIView file.
In the VC file, I added the UIView file like below.
class MyViewController: UIViewController {
private var myView = MyView()
…

Yuuu
- 715
- 1
- 9
- 32
3
votes
1 answer
Adding a target to a selector belonging to a different object
I have a UIViewController like this:
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView?
var backButton: UIButton?
override func loadView() {
self.webView = WKWebView()
self.backButton =…

RobertJoseph
- 7,968
- 12
- 68
- 113
3
votes
2 answers
Swift addTarget Error on TouchUpInside
I add a button on the view and bind event use addTarget , to call self.testp , but while I'm run it , an Error occured:
2015-06-19 23:08:29.237 UI[16978:1700826] -[UI.ViewController testp:]: unrecognized selector sent to instance…

Mofei Zhu
- 199
- 3
- 9
2
votes
2 answers
addTarget on a Custom UI Button not working programmatically
I created a custom UIButton with this initialiser :
class CustomButton : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
setUpButtoninClass(frame)
addTarget(self, action: #selector(handleTap), for:.touchUpInside…
user13370778
2
votes
1 answer
should I don't use addTarget of UIButton in viewDidLoad
My senior was reviewing my code and he found that I have used UIButton addTarget method like this
override func viewDidLoad() {
super.viewDidLoad()
self.btnAccount.addTarget(self, action: #selector(Accounts(_:)), for: .touchUpInside)
…

Devil Decoder
- 966
- 1
- 10
- 26
2
votes
1 answer
How to add a default parameter value in the #selector
I have a function:
func handleChange(isShown:Bool){
if isShown {
// do something A
}else{
// do something B
}
}
If I want a UISlider to add the above action with the isShown defaulting to false, and…

Ringo
- 1,173
- 1
- 12
- 25