I’ve got classes A, B & C in an iPhone project. While the iPhone app is running, either class (A or B) can instantiate an object of class C. How can I know in class C, which class instantiated the object (A or B)? Is there any way of knowing without adding an ivar to C? I tried using superclass but I’m either using it wrong or it’s not meant for what I’m trying to do.
Thanks!
Superclass is what the object inherits from, not the creator of an object.
One way to communicate back is through a delegate – can you get (A or B) to set the delegate property on C? That is kind of like the ivar you want to avoid, but using a protocol it is considered fine OO code.
It would be good to know why C needs to know. Does he need to send a method? Delegation is great for that. If not, then using custom init methods as mentioned by William Bonar is a possibility. If you are working with ViewController then you have methods such as presentingViewController, etc.
Good luck,
Damien