0
votes

This one's trickier to explain: I have ClassA which has MethodA, that does some stuff to some objects in ClassA, let's say it sets a couple of labels.

ClassA has also created an instance of ClassB, which is a sidebar view. I need ClassB to perform the same stuff to objects in ClassA as MethodA, updating those labels inside that instance of ClassA.

I need ClassB to be able to call MethodA, but have it act on the specific instance of ClassA that created that instance of ClassB.

The classes (at present at least) do not inherit from one another, since they don't actually share anything yet. I fill some data from ClassA into ClassB's labels, and now I need to do the opposite.

I can't call [super MethodA] from within ClassB, because they don't inherit. What I need is something analogous to a [parent methodA], which would call that method in the class that created this ClassB object, and have it act on that specific instance of ClassA.

Does such a thing exist? Apologies, jumbled post, and I'm not sure what to search for for a vague question like this.

2

2 Answers

0
votes

You need to create this yourself. The general pattern is to create a listener assignment in your child class, and the "parent" (more generically, the object wanting to receive notification) calls the method you establish to assign a listener. You can allow your child to maintain multiple listeners if you wish, or only a single listener. The listener should implement some protocol that is appropriate for how you plan to pass data.

0
votes

No, if you are using composition then you need an external way to refer to the class the contains the client one.

The easier way to do it would be to pass the parent while instantiating the object:

@interface ClassB {
  ClassA *parent;
}

-(id)initWithA:(ClassA*)parent;

@property (readonly, retain) ClassA *parent;

@end


@implementation ClassB

-(id)initWithA:(ClassA*)parent {
  if ((self = [super init])) {
    self.parent = parent;
  }

  return self;
}

@end

so that you can have in ClassB:

-(void)method {
  [parent updateThings:params];
}

Mind that in this case, since both class declarations are cross referenced you will need a forward declaration (eg. @class ClassA) in header file to make it compile.