views:

43

answers:

1

Hi all,

i have 3 viewcontrollers in each tab. i want to add a subview on top of each viewcontroller. there is a button in subview (i made an UIView with a button)

when i press the button in subview, how to call a method in ViewController?

here is the layout: ViewController1 has method1 ViewController2 has method2 ViewController3 has method3

if the user press the button (subviews button) in viewcontroller1, how can i call method1? is there any simple example?

A: 
[yourButton addTarget:ViewController1 action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];

You should keep a reference of the ViewController1 in the UIView inherited class. If you do not have a class for the UIView, and you do all the work in the ViewController1 class it would be:

[yourButton addTarget:self action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];
toupper
I have a class for UIView. i will use (add) it to my 3 different viewconrollers. you say addTarget:ViewController1, how do i know that button pressed in which uiviewcontroller? it can be ViewControler1 or 2 or 3.
tester
Are you trying to add the same instance of the view to the three differents viewControllers? If so, It is not possible according to the Apple doc. Views can have only one superview.If the instances of the views are different you could add a ViewController property in the UIView class set to the one in wich the UIview is.
toupper
yes, i want to create instances of the UIView (it has 3 buttons) for each ViewControls, i think i should add it with addsubview method.let me explain simple: all ViewController has a method named sayHello. and there is one UIView that has 3 buttons. i want to add an instance of UIView to all 3 ViewControllers. when i create an instance for UIViewController1, i want to send a varible to UIView. 3 buttons' tag should be 1, 2, 3. and when i press the buttons (on instance of uiview in viewcontroller1), how can i write [sender tag] to Label in ViewController1? same thing for other ViewControllers.
tester
is it so hard or apple doesnt allow it?
tester
if you do like this:[yourButton addTarget:ViewController1 action:@selector(method1:) forControlEvents:UIControlEventTouchUpInside];And declare the method1: - (void) method1:(id)sender{...}you are sending the pressed button as the first argument of the method.
toupper