How can I get UIButton to execute a class method while listening?

How can I make UIButton execute my method of class +doSomething:(id)sender in class MyClass after any event, for example UIControlEventTouchDown ? Is it possible to use -addTarget:action:forControlEvents: like I, for example, methods?

Thanks in advance.

+4
source share
2 answers

Since classes are also ObjC objects, you can use -addTarget:... , as usual:

 [button addTarget:[MyClass class] action:@selector(classMethod:) forControlEvents:UIControlEventTouchUpInside]; 
+11
source

Add an intermediate method that will call the class method.

 [button addTarget:self action:@selector(callClassMethod:) forControlEvents: UIControlEventTouchDown]; - (void)callClassMethod:(id)sender { [MyClass classMethod:sender]; } 
0
source

Source: https://habr.com/ru/post/1300842/


All Articles