2

I have a "NewsViewController" which has a property named "postViewController" of type PostViewController. From inside the PostViewController I would like to call a method of the NewsViewController class. What is the easiest way to do this ?

After googling that question, it seems like I should use a delegate or send a notification but I was hoping there would be a simpler way to do this like [self.parent parentMethod]...

Please tell me there IS a simpler way to do this! :) (and if there isn't please explain why!)

teum
  • 125
  • 3
  • 12
  • You pretty much covered all the common ways to do it, passing in the parent object and using [self.parent parentMethod] probably isn't the best most extensible way to do it but it works and really that's the most important thing in the end. I would recommend learning the delegate pattern or the observer pattern (first two you mentioned) as they become invaluable tools for later. – Joe May 26 '11 at 17:06
  • What do you mean by "passing in the parent object" exactly ? – teum May 28 '11 at 12:36
  • Can the method be static in the NewsViewController? If so, then you won’t need an instance of it in the PostViewController. Also, if the method can be static, maybe it should be refactored into utility or helper class to reduce coupling. – johnnieb May 29 '11 at 19:29

2 Answers2

0

it seems like I should use a delegate or send a notification but I was hoping there would be a simpler way to do this like [self.parent parentMethod].

That is a delegate, at its most basic level. Create a property on the child object of a type that handles parentMethod messages. Your parent assigns self to it after instantiating the child object or your child object exposes an init... method that lets you pass it in.

Jim
  • 72,985
  • 14
  • 101
  • 108
-1

Use the super keyword: [super parentMethod]

See this answer: What exactly is super in Objective-C?

Community
  • 1
  • 1
johnnieb
  • 3,982
  • 4
  • 29
  • 32
  • That's for subclasses explicitly calling their superclass. Part of the problem with this question is the definition of the term "parent" and "child". Could be he means view and subview, or viewControllers on a navigationController stack... – Dan Ray May 26 '11 at 17:10
  • Yes, my question wasn't clear enough, that's why I rephrased it. And you are right I don't want to call a method of the superclass from a child class. I need to call a method of an object from a property of it...I'm having a hard time writing it correctly, reading my edited first post may help. – teum May 28 '11 at 12:39
  • The question has completely changed. I don’t think I deserved the vote down. – johnnieb May 29 '11 at 18:50