Add userInfo dictionary to view controller segue mechanism
| Originator: | davidahouse | ||
| Number: | rdar://21434280 | Date Originated: | 6/17/2015 |
| Status: | Open | Resolved: | |
| Product: | iOS SDK | Product Version: | any |
| Classification: | Enhancement | Reproducible: | Always |
Summary: Dependency injection between View Controllers is a preferred mechanism for passing application state (as opposed to singletons). This is complicated when using segues because a developer must implement the prepareForSegue: method and assign properties on the destination view controller. This takes the loose coupling that segues are supposed to give us, and adds a layer of tight coupling on top. This leads to more complex code because the place in the code that initiates the segue is now completely different from the code that provides the dependency injection. When using storyboards, this issue remains complex because the segue is defined visually (from the action of a button for example), while code must still be written to correctly give the destination view controller enough information to display correctly. One suggestion to fix this problem is to create a userInfo dictionary (similar to other APIs such as NSNotification) that is present on every UIViewController. By adding a parameter to the associated presenting methods such as performSegueWithIdentifier:sender such as userInfo, the code that initiates the segue can simply fill a dictionary with any data that the destination view controller needs. Once in the destination view controller viewDidLoad: or viewDidAppear: it can access the userInfo dictionary to access the information. In addition, a viewDidSegue: could be added to specify that the view was presented using a segue. In addition, all of the methods that present view controllers such as presentViewController:animated could include a userInfo dictionary as well. This makes the process consistent across any of the ways that view controllers are presented. Finally, a way to map a dictionary object to a segue visually would allow developers to use segues visually in a storyboard (not just from code). Once we have a way to pass data between view controllers, segues really do give us a clear way to manage all the transitions and user actions in our applications. Until then, developers must rely on using code only approaches to simplify and keep code maintainable. Steps to Reproduce: 1. Try to pass data with a segue without implementing prepareForSegue: Expected Results: There should be a parameter for passing data to the destination view controller automatically. Actual Results: There is no way to do this without writing code in prepareForSegue and introducing a tight coupling (which segues are meant to eliminate).
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!