Zach Olivare - 2015 Apr 19
When a navigation controller is used to transition the user to a different view where they need to make a selection, how can this selection data be transferred back to the main view?
Talking with Dr. Sebern, we reached the conclusion that this is the type of problem that is generally handled with a delegate pattern. Apple's developer website describes deletation as "a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled".
Tutorialspoint.com gives a (now slightly outdated) walk through of creating a delegate in objective-C here.
These resources in tanduem with a great deal of help from Dr. Sebern, led me to use the following steps in order to make the delegate pattern work for me:
Note: In the following instructions, the main view will be represented by the class mainVC, and the selection view will be represented by the class selectionVC.
The first step is to create a protocol which represents your delegate. This protocol will have one function, which is called by the delegating object on the delegate to inform the delegate of the change.
After the delegate procol is implemented, a delegate property needs to be added to the delegating object (selectionVC), and initialized to nil:
var delegate: CategoriesControllerDelegate? = nil
The delegate (mainVC) then needs to implement the delegate protocol that was created:
At some point prior to its invocation, the delegate reference contained by the delegating object needs to be set to the actual delegate object. In this example, it makes sense to do this immediately before the selection view is shown by the navigation controller. For this reason the processing is done inside of the