Passing data between view controllers
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with multiple view controllers in iOS applications, it is essential to pass data between them efficiently. This ensures that the app's functionality and user experience remain seamless and intuitive. In iOS development, data passing can be achieved in several ways depending on the scenario and the architectural pattern you are using (e.g., MVC, MVP, MVVM, or VIPER). Below, we explore common methods such as segue, delegates, closures, notifications, and data stores, supplemented with examples in Swift, the most widely-used language for iOS development.
1. Segue-based data passing
When using storyboards, segues are the most straightforward method for passing data directly between view controllers. A segue is triggered when transitioning from one view controller to another, and you can pass data in the prepare(for:sender:) method.
Example:
In this example, data is assigned directly to a property of the destination view controller before the transition.
2. Using Delegates
Delegates are a design pattern that allows one object to send messages to another object when specific events happen. It's a powerful tool for passing data back from a destination view controller to the source view controller.
Example:
3. Closure Callbacks
Closures can be used to create a callback mechanism to pass data. This is particularly useful for more complex data passing scenarios or when using programmatic UI code instead of segues.
Example:
4. Notifications with NotificationCenter
NotificationCenter is a way to implement a broadcast mechanism, where one view controller can send notifications that multiple classes can listen to.
Example:
5. Shared Data Stores
Using a shared data store or a singleton pattern, such as a centralized model, can be effective if many view controllers need access to the same data. However, this approach can lead to tightly coupled code and should be used with care.
Summary Table
| Method | When to Use | Complexity | Data Flow Direction |
| Segue | With Storyboards | Low | Forward |
| Delegates | For customized back data passing | Medium | Backward (mostly) |
| Closures | Highly customizable scenarios | Medium | Both |
| Notifications | Broad, app-wide events | Low | Both |
| Data Stores | Access from multiple points | Medium | Both |
Additional Considerations
- Memory Management: When setting up closures and delegates, handle memory cycles carefully using
[weak self]in closures and declaring delegate properties withweak. - Modularization: Each method has its scenario; consider the project structure and future maintenance when choosing a method.
- Performance: Note that NotificationCenter can lead to high coupling and obscure logic paths, which may impact performance and debuggability.
Through these techniques, iOS developers can ensure their applications manage data flow cleanly and predictably across different parts of the application.
Related reading
- Passing data between view controllers
- Passing data between view controllers
- Passing enum or object through an intent the best solution
- Passing parameters to addTargetactionforControlEvents
- Passing through touches to UIViews underneath
- Password hint font in Android
- Paste text on Android Emulator
- Percentage width in a RelativeLayout
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.