Is it worth storing references to other viewcontrollers that the current viewcontroller opens? If I have one viewcontroller displaying a screen with a button that opens a popoverviewcontroller, should I then keep a reference to it and then check afterwards to see if the variable is nil to only make the popoverviewcontroller once? Or is it unnecessary? It lead to many extra variables in some cases, and additional code, but it would be great if someone could say if this is unnecessary or not.
if (self.popoverVC == nil) {
UIPopoverController *popVC = [[UIPopoverController alloc] initWithContentViewController:self.configureTest];
popVC.popoverContentSize = CGSizeMake(320.0, 460.0);
popVC.delegate = self;
self.popoverVC = popVC;
[popVC release];
}
Storing references to other controllers is fine with me and I do it whenever it seems reasonable to me. Think of the delegate pattern as a way to model the interaction between your controllers.
There are alternatives, though.
In you specific case, instead of storing the reference to the controller, you could set a flag (but I don’t know if this makes sense for your specific workflow) specifying that the button was pressed.
Not applicable in your case but possibly useful in general is accessing the “parent” hierarchy of
UIViewControllers. This is not explicitly maintained by UIKit, so either you set theparentViewControllerproperty or you can go from one controller to its parent navigating theUIResponderhierarchy. This is not applicable to your case, it seems to me, because communication would be initiated by the child controller (in your case you are interested in the opposite). Anyway, google for it, if you are interested.Another option you have is using notifications: one controller can register for notifications, another can observe it.
This is a bit more expensive than a function call, but it requires no ivar and no coupling between controllers.