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 developing iOS applications, one common task is to navigate between different screens or views, often encapsulated by view controllers. Passing data between these view controllers is crucial for maintaining a fluid and responsive user experience within an app. This article dives into several methods available in iOS development to transfer data between view controllers, with detailed technical explanations and examples where relevant.
Methods for Passing Data Between View Controllers
Whether you're using storyboards or programmatically managing view controllers, there are several methods for passing data:
- Segues (Storyboards)
- Delegate Pattern
- Callbacks/Closures
- NotificationCenter
- Singletons
- Property Injection
Let's delve into each of these methods.
1. Segues (Storyboards)
In iOS, segues define a transition between two view controllers. Using segues with storyboards, you can pass data by overriding the prepare(for:sender:) method. This method is called just before a segue is executed.
2. Delegate Pattern
The delegate pattern is a design pattern where one object in a program acts on behalf of, or in coordination with, another object. It is a powerful way to adopt single responsibility and separation of concerns principles in your app's architecture.
3. Callbacks/Closures
Closures in Swift are similar to blocks in C and Objective-C or lambdas in other languages. They can be used to pass data back to a previous screen.
4. NotificationCenter
NotificationCenter is a powerful way to broadcast information within your app. It enables you to send messages across the app without directly linking the sender and receiver.
5. Singletons
Singletons ensure that a class has only one instance and provide a global point of access to it. While considered an anti-pattern by some due to its potential for misuse and difficult testing, it is a simple way to share data.
6. Property Injection
Property injection involves setting properties directly on a view controller before navigation. This is a straightforward way to pass data.
Summary
Here's a summary of the different methods for passing data between view controllers:
| Method | Code Simplicity | Use Case | Notes |
| Segues | Medium | Use with storyboards. For simple data passing between two controllers. | Integrated with storyboards. |
| Delegate Pattern | Advanced | When a child view controller needs to send data back to its parent. | Promotes loose coupling. |
| Callbacks/Closures | Medium | For callbacks after an action has occurred, e.g., completion handlers. | Powerful and concise but may cause retain cycles. |
| NotificationCenter | Advanced | Broadcasts data to multiple view controllers. | Suitable for global notifications. |
| Singletons | Easy | Sharing global application state/data. | Can lead to tightly coupled code. |
| Property Injection | Easy | Simple direct data passing during navigation. | Best for direct property setting. |
Each method has its merits and is suitable for different scenarios in app development. Understanding these concepts will help you to make informed decisions when architecting interactions between view controllers in your iOS applications.
Related reading
- 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
- Perform Segue programmatically and pass parameters to the destination view
- Perform UI Changes on main thread using dispatch_async or performSelectorOnMainThread?
.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.