views:

35

answers:

3

Hello. I have a ViewController which has a button that when is pressed adds a subview from nib. I have this action:

- (IBAction) addTooltip {
    if (self.tooltip == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Tooltip" owner:self options:nil];
        self.tooltip = [nib objectAtIndex:0];
        self.tooltip.frame = CGRectMake(20, 190, 260, 200);
    }
    [self.view addSubview:tooltip];
}

In this nib i have other 2 buttons but i want to catch their actions in my ViewController. Is it possible? Or how's the best approach to do this? The tooltip is of type Tooltip, so a i have a class for it.

A: 

Uhm, use delegates? Add a delegate method to your ViewController and in the Tooltip add a method that sets the delegate to send actions to.

Think of it like having a plug on the Tooltip and a socket on the ViewController. When you add the Tooltip you are plugging it into the ViewController so they can know about each other.

ExitToShell
+1  A: 

In the Tooltip nib, you can set the nib's owner to your ViewController and wire the buttons action up to the ViewController.

Edit:

In InterfaceBuilder, you can set the class of the "File’s Owner" owner to the custom class of your ViewController. To do that,

  1. in IB, in the document window (⌘0), click on the "File’s Owner",
  2. in the Identity Inspector (⌘4), set the Class to your custom class (just type the name),
  3. right (control) click on the "File’s owner" in the document window to see the declared actions and outlets of your class.
Nikolai Ruhe
I think i'm already adding the owner to my viewcontroller (owner:self), i don't see any other place to do that.But the problem is how do i wire my buttons from Tooltip nib to my ViewController nib? It doesn't seem to be possible.
Cristi Băluță
A: 

I added this line in my initial function and works

[self.tooltip.butCancel addTarget:self action:@selector(removeTooltip:) forControlEvents:UIControlEventTouchUpInside];

If there's a better way please give some examples also, i didn't understood how to use delegates. Thanks.

Cristi Băluță
The easiest way is to use IB. I edited my original answer to make clear how that works.
Nikolai Ruhe