0

I have hit a wall with my design. I am trying to pass the strings from one View Controller to another. Those 2 controllers are not connected with segue. I have attached the screenshoot of my Storyboard. I have a feeling that my only option is to use a Singleton, or would that be possible with delegates? without using the method:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 

I can not acces the VC1 by the segue identifier. How would that be possible without accessing the identifier?

Please have a look at the image and let me know what options do I have and what would be the best to use here.

Storyboard design

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tom
  • 27
  • 1
  • 3
  • 7
  • If you do not want to use a Singleton, then you could use CoreData. VC1 would store whatever information you are dealing with into a Table of coreData, and then VC2 would retrieve it in the `viewWillAppear` method. Another option would be UserDefault... the best option really depends on which data you want to pass – nikano Mar 06 '18 at 20:18
  • If the view controllers aren't connected to each other (or even if they are), you should really consider whether they should be talking to each other directly. Especially in cases where you're passing data around, you should use some sort of data model that's separate from the controllers. – Caleb Mar 06 '18 at 20:57

3 Answers3

5

If you navigate programmatically to your second VC you can set Values like so (3rd line)

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController

newViewController.stringVariable = stringVariable

self.present(newViewController, animated: true, completion: nil)
Louis Eppler
  • 223
  • 1
  • 8
  • This has limitations as the `newViewController` will not be the same instance of the `UIViewController` that the user goes back to within the navigational hierarchy. – NSGangster Mar 06 '18 at 20:46
1

If its a small data like some string or variable you can use UserDefault or pass data using that class variable like vc2.data = data How to use UserDefaults How to use UserDefaults in swift?

If its more like table or number of user list you can use plist store in your bundle at can retrieve from any view controller using Plist How do I get a plist as a Dictionary in Swift?

if two controller are some how connected you can use delegates. delegats:- Delegates in swift?

You can also use notifications if you are not sure when data will be available/ or send base on some action, or send if something is trigged.

Pass data using Notifications How to pass data using NotificationCentre in swift 3.0 and NSNotificationCenter in swift 2.0?

Iraniya Naynesh
  • 1,125
  • 3
  • 14
  • 26
  • 1
    Thanks. The problem with user UserDefaults is that i need to pass that data in to second view controller first and then in second view controller i want the data to be saved with UserDefaults. Otherwise the second VC wouldn't make sense. The second VC function will be to save all that data. – tom Mar 06 '18 at 20:34
0

One way is to use the navigational heiarchy to access the view controller.

From the viewController that you want to pass data it's tabBarController property should be the same instance of the tab bar that VC2 is a part of

func setStringForVC2(stringToSet:String) {
    guard let myTabBarController = tabBarController, let tabBarViewControllers = myTabBarController.viewControllers else {
        return
    }

    //need to access index 1 since vc's are stored in tabbarcontroller starting with 0, so 1 to access 2nd vc.
    if (tabBarViewControllers.indices.contains(1)) {
        if let vc2 = tabBarViewControllers[1] as? VC2Class {
            vc2.stringIWantToSet = stringToSet
        }
    }
}
NSGangster
  • 2,397
  • 12
  • 22