Search code examples
swiftdelegation

Can a child view controller be the delegate of its parent view controller?


Lets say there are two classes, class A and class B. Class B is a child view controller of A.

protocol ClassADelegate: class{
     functions
}
class A {
     code
     add B as child view controller
}


protocol ClassBDelegate: class{
     functions
}
class B {
     code
}

Considering that class A is the parent view controller of B, My question is if class A is a delegate of class B and class B is a delegate of class A, does that create an ownership cycle? Is this bad practice?


Solution

  • The parent should be the child's delegate.

    The parent, if it needs to tell the child something, should just invoke methods.