Take the 2-minute tour ×

In the apps I've developed so far, I've always had a UIViewController in charge of presenting other UIViewControllers to display transitions in the UI. Whenever I've had a "managed" view controller call a method in the "managing" view controller, I've had to ensure that I had a reference to the managing controller inside of the managed controller.

For example: If I have a UIViewController called MainController that is in charge of managing other UIViewControllers in my application's UI, and MainController creates 2 controllers for presentation. Called SubController1 and SubController2 respectively. If SubController1 is finished and MainController needs to display SubController2, I've always set a MainController reference variable inside of SubController1 so that it could communicate the change to MainController. This means that whenever MainController creates SubController1 it has to set SubController1's MainController reference variable to self.

Should I be worried about memory usage with this method of communications? Would I be better off using notifications for this kind of behavior? The major benefit I've found with the method I'm using now is it makes it incredibly easy to share information between view controllers if necessary. Any thoughts are greatly appreciated.

share|improve this question
use customDelegate or NSNotificationsCenter –  Madhusudhan Apr 18 '13 at 13:25
add comment

1 Answer

up vote 2 down vote accepted

Handling changes, actions, or user interaction in other views is the essential use case of delegates. The best practice is to have the main controller be the delegate of the first/second view controller, and then as events happen in the child controllers, they call certain methods on their delegates, which can then take any necessary action.

Passing references the way you describe won't have a noticeable memory impact, however it does very tightly couple your code to the MainController, which should be avoided. You should strive for flexibility to handle potential changes in the future. What happens if you transition to a universal app and the iPad UI has a vastly different structure? With delegates, you only need to ensure that the contract between the controllers is intact, but are free to change any of the underlying implementation details.

Other options for keeping values/state in sync between views are:

share|improve this answer
add comment

Your Answer


