Add Selector to UIButton

10,153

Solution 1

Unless I'm misunderstanding the question, there's no trick to it.

If you define (IBAction)buttonPressed:(id)sender in the main view controller, it's inherited by the iPad view controller subclass. Therefore specifying it as a selector belonging to self when the iPad view controller sets up its button should work fine.

Solution 2

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.

Share:
10,153
darksky
Author by

darksky

C, C++, Linux, x86, Python Low latency systems Also: iOS (Objective-C, Cocoa Touch), Ruby, Ruby on Rails, Django, Flask, JavaScript, Java, Bash.

Updated on June 04, 2022

Comments

  • darksky
    darksky almost 2 years

    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.