passing data from view one to seven

Hi everyone ... i am pretty new to IOS dev, so any help would be great. I am trying to send a string "apple" from UIviewcontroller one to the UIviewcontroller sixth. I am able send the string "apple" from one UIviewcontroller to the next. But then i will have to repeat it again and again in all the views. Is there anyway i can send it directly to the sixth UIviewcontroller ?

Have a look at notifications. If you want to send "apple" from VC1 to VC6 you would 'post' the notification in VC1, and VC6 would be notified because it's registered to receive such notifications. For example, in VC1:

import UserNotifications

let kNotificationValue: String = "notificationValue"

// ... Various methods etc., then, post the notification:
NotificationCenter.default.post(name: .sendApple, object: nil, userInfo: [kNotificationValue : "apple"])

// blah, blah, blah

// Set up notifications names here so you don't introduce typos
extension Notification.Name {
	static let sendApple = Notification.Name("sendApple")
}

You make VC6 receive the notification like this:

// In your view's .onAppear method:
.onAppear {
    NotificationCenter.default.addObserver(forName: ..sendApple, object: nil, queue: nil) { notification in
    guard let value = notification.userInfo?[kNotificationValue] as? String else { return }
    print(value)
  }
}

Note that multiple VCs can be setup as observers of the notification, and VCs can observe multiple notifications (just add a new name to the extension).

thanks @darkpaw. Is there a time frame in which notification has to pass ? As VC1 to VC6 takes a while as i have image upload and data entry in other views.

// Post part

NotificationCenter.default.post(name: Notification.Name("PassData"), object: nil, userInfo: ["data": "apple It is"])

// Destination Part

override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(:)), name: Notification.Name("PassData"), object: nil) } @objc func handleNotification( notification: Notification) { if let data = notification.userInfo?["data"] as? String { print(data) }

--------- I am still unable to get the result printed (print data). Does notification works if there is multiple views between them.

@sonam93 Yes, that requires that observer is added when notification is sent

See details here: https://stackoverflow.com/questions/66489663/nsnotification-not-observing-or-posting-data

Where and when do you load VC1 to VC6 ?

If it is just a timing delay, you could post the notification in a dispatch with a 0.2s delay. That should work (increase the delay a little bit if needed).

DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
  NotificationCenter.default.post(name: .sendApple, object: nil, userInfo: [kNotificationValue : "apple"])
}
passing data from view one to seven
 
 
Q