سؤال

I have two childViewControllers of the same parent, say methodChild and propertyChild, and want a method in methodChild to be called every time a new value is set in one of propertyChild's properties. What is a good way to do this?

One solution that occurred to me was this:

  • In the property setter, find the other child (perhaps through the parentViewController method and childViewControllers array) and ask it to perform the method.

My main worry with this sort of thing is accidentally creating a retain cycle or a memory leak of some sort, so any advice on this would be terrific. Thanks for reading!

هل كانت مفيدة؟

المحلول

The parent can hold references and keep track of the data that changes. It can simply call exposed update methods of its children.

The child controllers should use delegate protocols to inform the parent of changes. The delegate is weak reference to the parent in the form id <ChildDelegate>. This is to avoid the retain cycle problems you are afraid of.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top