When communicating with parent objects, you can select several design patterns. Delegation and notification are a good choice.
The big idea here is communication with a free connection. Notifications use Singleton to handle communications, while delegation uses weak references to parent objects. (Check out Cocoa With love: keep cycles )
If you go with a delegation, you can create an unofficial protocol for your ViewControllerA, which the MainViewController must comply with.
You can call it the ViewControllerADelegate protocol:
@protocol ViewControllerADelegate @optional - (void)bringSubViewControllerToFront:(UIViewController*)aController; @end
Or ViewControllerA may send a notification:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyFunkyViewSwitcherooNotification" object:self];
And MainViewController should listen if it wants to know:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(swapThoseViews:) name:@"MyFunkyViewSwitcherooNotification" object:nil];
source share