Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between tab viewed controllers in swift?

Tags:

What it does

  1. When the first page in my tab bar controller loads, I retrieve data from a json file
  2. I store it in an array (in the first view controller)
  3. The data obtained will be displayed in the second view controller. The data is already loaded and stored in an array in the first view controller.

Problem:

I can't figure out a way to pass the data between the two view controllers. Can't pass data based on the segue identifier since it is a tab bar controller

Please help!

like image 599
0to1000 Avatar asked Dec 25 '14 23:12

0to1000


People also ask

What is UIViewController in Swift?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

How to send notifications to multiple view controllers in Swift?

To handle the notification when it gets sent, you should implement a method and use it as the selector parameter for the addObserver implemented above: Finally, to send the notification from another View Controller, just use the post method with your notification name: So, these were five ways of passing data between View Controllers in Swift.

How do I pass data between view controllers?

Photo by the author. In this article, we’re going to explore five ways to pass data between View Controllers, with Swift code snippets and examples. The five ways are: 1. Segues Segues are a storyboard mode to pass data. Imagine your app has an onboarding screen, and you want to ask the user's name to address them later on another screen:

What happened to tabbarcontroller in Swift?

It is now upgraded to Swift 1.2. Most places where I downcasted from tabBarController with as is now as! Excellent! I’ve been working with a tab bar controller with 3 tabs and was using global variables to store some default settings and such, but knew there must be a better way and this is it. So thank you for laying it out very clearly. :)

How do I create a single view controller in Swift?

Make a new project by either going to File>New>Project… or Command-Shift-N on the keyboard. Make a single view project called SwiftTabDataOrderDemo. Make the language Swift and the device Universal. Save the project. Go to the storyboard. Select the single view controller by clicking on the title.


1 Answers

If you need to pass the data between view controllers then :

var secondTab = self.tabBarController?.viewControllers[1] as SecondViewController secondTab.array = firstArray  
like image 85
H. Serdar Çınar Avatar answered Oct 20 '22 22:10

H. Serdar Çınar