问题
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.
回答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.
回答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
.
来源:https://stackoverflow.com/questions/10758926/add-selector-to-uibutton