1

I hope I can make this clear. I have button and I want to know what function in what object is the target for it's .touchDown event. There are properties and functions in iOS that might be what I'm looking for, but I can't make them work. The property is: self.allTargets the function is: self.actions(forTarget:, forControlEvent:)

I've printed the values with this code:

print( self.allTargets )
print( self.actions(forTarget: self, forControlEvent: .touchDown) ?? "no targets")
print( self.actions(forTarget: Button_Test.ViewController , forControlEvent: .touchDown) ?? "no targets")
print( self.actions(forTarget: "ViewController", forControlEvent: .touchDown) ?? "no targets")

result:

[AnyHashable(<Button_Test.ViewController: 0x7b293300>)]
no targets
no targets
no targets

I can see the button has a target, but I can't get the function. Help!

S'rCat
  • 468
  • 1
  • 4
  • 16

3 Answers3

1

To get all target/action pairs for a given control event (.touchDown in this case), you need to do the following:

let targets = self.allTargets
for target in targets {
    if let actions = self.actions(forTarget: target, forControlEvent: .touchDown) {
        for action in actions {
            print("taget: \(target) - \(action)")
        }
    }
}

Basically you need to iterate through all of the targets and get the list of actions for each target for the given event.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

I think i understand you a little bit and i think you may have some confusion over the button target thing, so i will explain the following things and hopefully it can help you find what you want.

let button = UIButton()
button.addTarget(target: self, action: #selector(randomFunction) for: .touchInsideUp). 

The target the button adds, is the target that it wishes to get the function "randomFunction" from.

So if you are coding in, let's say, MyViewController, then specifying target to be self, we are saying to the Button that:"get the selector function from MyViewController".

.touchInsideUp is the event that will trigger the Button to execute the function in the selector, in this case, it is the "randomFunction". But in order for you to know where to find the function, that is the reason why you need to set the "target".

Now, since the compiler knows when it should execute a function (.touchInsideUp, for example), and it also knows which controller it should look to (MyViewController), it will then execute the randomFunction() in MyViewController whenever you do a .touchInsideUp action to the button.

Hopefully it helps.

user3608914
  • 136
  • 1
  • 7
0

It seems like the target AnyHashable(<Button_Test.ViewController: 0x7b293300>) is registered to execute an action for one or more control events, but assuming that self == the Button_Test.ViewController at memory address 0x7b293300, it's just not registered for the particular control event .touchDown

The most common control event for UIButtons that triggers an action is .touchUpInside. Have you tried specifying that event, e.g.:

print( self.actions(forTarget: self, forControlEvent: .touchUpInside))

The other possibility is that self is not the registered target. What does the console say if you add this line:

print(self)

Is the type of self Button_Test.ViewController and is the address 0x7b293300?

As a note, your third and fourth print() statements will never return a valid result, because they pass in targets that don't match the actual target that exists on your buttons (in one case just the type Button_Test.ViewController, and in the other case a string "ViewController").

Daniel Hall
  • 13,457
  • 4
  • 41
  • 37