{"id":1995,"date":"2025-08-05T13:19:39","date_gmt":"2025-08-05T13:19:39","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1995"},"modified":"2026-02-05T11:59:53","modified_gmt":"2026-02-05T11:59:53","slug":"pass-data-in-swift","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/pass-data-in-swift\/","title":{"rendered":"How to Pass Data From One VC to Another in Swift?"},"content":{"rendered":"\n<p>Data transfer in Swift refers to how you pass and share data between:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>View Controllers<\/li>\n\n\n\n<li>Views and ViewModels<\/li>\n\n\n\n<li>Screens\/Modules<\/li>\n\n\n\n<li>Background tasks and UI<\/li>\n\n\n\n<li>Across the app (via singletons, UserDefaults, etc.)<\/li>\n<\/ul>\n\n\n\n<p>Swift offers multiple techniques, depending on context (UIKit, SwiftUI, architecture). Here&#8217;s a full breakdown:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Passing Data Between View Controllers (UIKit)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 Forward Passing (Parent \u27a1\ufe0f Child)<\/h3>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProfileViewController: UIViewController {\n    var username: String?  \/\/ \ud83d\udd39 Property to receive data\n}\n\nlet profileVC = ProfileViewController()\nprofileVC.username = \"shomil\"  \/\/ Pass data before push\nnavigationController?.pushViewController(profileVC, animated: true)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 Backward Passing (Child \u27a1\ufe0f Parent)<\/h3>\n\n\n\n<p><strong>Using Delegate Pattern:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protocol SettingsDelegate: AnyObject {\n    func didUpdateTheme(to darkMode: Bool)\n}\n\nclass SettingsViewController: UIViewController {\n    weak var delegate: SettingsDelegate?\n\n    func toggleTheme() {\n        delegate?.didUpdateTheme(to: true)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 Using Closures (Callback)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class DetailViewController: UIViewController {\n    var onDataUpdate: ((String) -> Void)?\n\n    func saveChanges() {\n        onDataUpdate?(\"Updated Data\")\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Using Segues (Storyboard)<\/h2>\n\n\n\n<p>In prepare(for:sender:), set data before navigation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {\n    if let destination = segue.destination as? DetailVC {\n        destination.data = \"Some text\"\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Using ViewModel (MVVM)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>In MVVM, data flows from Model \u27a1 ViewModel \u27a1 View\nclass UserViewModel {\n    let name: String\n\n    init(user: User) {\n        self.name = user.firstName + \" \" + user.lastName\n    }\n}\n\nstruct UserView: View {\n    let viewModel: UserViewModel\n    var body: some View {\n        Text(viewModel.name)\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Using Singleton (Global Access)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class AppData {\n    static let shared = AppData()\n    var isLoggedIn = false\n}\nAccess from anywhere:\nAppData.shared.isLoggedIn = true<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. Using NotificationCenter<\/h2>\n\n\n\n<p>Post and listen for broadcast messages (loose coupling):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NotificationCenter.default.post(name: .dataUpdated, object: \"New Value\")\nNotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: .dataUpdated, object: nil)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. UserDefaults \/ Keychain (Persistent Transfer)<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UserDefaults: <\/strong>For small, non-sensitive data<\/li>\n\n\n\n<li><strong>Keychain: <\/strong>For secure data like tokens, passwords<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>UserDefaults.standard.set(\"Shomil\", forKey: \"username\")\nlet name = UserDefaults.standard.string(forKey: \"username\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. API\/Network Transfer<\/h2>\n\n\n\n<p><strong>For remote data transfer (JSON, REST APIs):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>URLSession.shared.dataTask(with: url) { data, response, error in\n    \/\/ Parse JSON, return model\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">8. Data Transfer in SwiftUI<\/h2>\n\n\n\n<p>Use @State, @Binding, @ObservedObject, @EnvironmentObject to pass and share data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct ChildView: View {\n    @Binding var isOn: Bool\n}\n@ObservedObject var viewModel = ProfileViewModel()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Swift gives you many ways to pass data, from simple property setting to advanced patterns like delegates and closures. Choosing the right method depends on direction, complexity, and architecture. To implement clean and efficient data flow, you can <a href=\"https:\/\/www.cmarix.com\/hire-swift-developers.html\">hire Swift developers<\/a> who are experienced with both UIKit and SwiftUI patterns.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data transfer in Swift refers to how you pass and share data between: Swift offers multiple techniques, depending on context (UIKit, SwiftUI, architecture). Here&#8217;s a full breakdown: 1. Passing Data Between View Controllers (UIKit) 1.1 Forward Passing (Parent \u27a1\ufe0f Child) Example: 1.2 Backward Passing (Child \u27a1\ufe0f Parent) Using Delegate Pattern: 1.3 Using Closures (Callback) 2. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1996,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,161],"tags":[],"class_list":["post-1995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile","category-swift"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1995"}],"version-history":[{"count":3,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1995\/revisions"}],"predecessor-version":[{"id":2000,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1995\/revisions\/2000"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1996"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}