Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Selector to UIButton

I have ViewController and then two different ViewControllers that extend that main ViewController, one for the iPhone and the other for the iPad.

The iPad's ViewController instantiates a separate extended UIView and sets it as its own view. That view has some buttons, which I want to add its selector methods as some methods in the main ViewController. How can this be achieved?

So here's a way to visualize this:

Main ViewController
| iPhone ViewController
| iPad ViewController
  | Some UIView Class    --> Button must invoke method in Main View Controller

EDIT: I do not use interface builder at all.

like image 660
darksky Avatar asked Nov 28 '22 09:11

darksky


1 Answers

Your question is a bit confusing, but if I understand, you can use code like this to setup the button:

[button addTarget:yourObject action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];

So you set addTarget to the instance of MainViewController, and set the action to whatever the method is you want to call inside MainViewController. You should be able to do this in Interface Builder too as long as your method accepts IBAction.

like image 188
woz Avatar answered Dec 09 '22 14:12

woz