Reputation: 33
As the title states, I'm looking to simply pass the information.
I have a tabbed view application currently, and the user inputs data into a text field, presses a button, then labels are filled with the entered text on the same viewcontroller.
I want to send that information to the other tab and fill a label.
I know I can do this via protocol or segues, however, I want to remain on the current tab. I haven't seen this as an example anywhere, only to switch the view to the other screen.
Anyone know how to simply pass the string entered and not change the view?
Upvotes: 1
Views: 337
Reputation: 318854
You are looking at this all wrong. You do not want or need to pass data between view controllers. If your app makes proper use of MVC (model, view, controller), then what you should be doing is updating a model. That model should broadcast that is has been updated. Anyone that cares about the model should react to those notifications as needed.
You have a tab controller with multiple view controllers. Two or more of your view controllers have an interest in the same data model. Both should reference the same instance of the data and be setup to be notified about changes to that instance of the data model.
One view controller, through its views, updates the data model. The data model then sends out a notification that is has been updated. Now the interested view controllers receive this notification and update their own views based on the updated data model.
No view transitions required. No segues required. No communication between different view controllers required.
Look at the documentation for NotificationCenter
for ways to broadcast messages and for ways to listen for such messages.
Upvotes: 5