开发者

dispatch events to parent views

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);
  开发者_StackOverflow中文版  }
    [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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜