1

it is more curiosity than a real problem because it works. But i don't understand xcode warning. In my application I use addChildViewController so i have a child and a parent. In my child, I had a UIButton and i wanted to event a parent's function : it works but xcode says undeclared selector parentFunction :/ I can declare this function to erase this warning but it will be dead code (bad way). Or I can use a delegate, but why use delegate when you have a direct link.

[btn addTarget:self.parentViewController action:@selector(parentFunction:) forControlEvents:UIControlEventTouchUpInside];    //undeclared selector parentFunction

Best regards

Thx

rmaddy
  • 307,833
  • 40
  • 508
  • 550
Charlu
  • 290
  • 2
  • 12

1 Answers1

4

You need to import the parent view controller's .h file, and cast self.parentViewController to that class or the compiler doesn't know that method exists (since parentViewController returns a UIViewController, not your custom controller).

[btn addTarget:(ParentClass *)self.parentViewController action:@selector(parentFunction:) forControlEvents:UIControlEventTouchUpInside];

Replace ParentClass with whatever the class name of that parent is. Also, make sure that parentFunction: is implemented in the parent class (and has the colon), and is declared in its .h file.

rdelmar
  • 103,582
  • 11
  • 205
  • 218