1

I currently have three Swift files, one for the main view in a ViewController, and two more which are used for the two views within the first view, which are used for a Segmented Control.

As these don't use segues between each other, I can't use the prepareForSegue method to transfer data between them, so how do transfer the variables and such from one file to another?

This doesn't seem to be a duplicate as other cases such as the one commented are using segues, mine is not.

Ethan Humphries
  • 1,786
  • 3
  • 19
  • 28
  • Possible duplicate of [How to pass prepareForSegue: an object](http://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object) – dahiya_boy Feb 01 '17 at 11:30

7 Answers7

2

Are all three Swift classes view controller subclasses?

You have your main view controller with your segmented control. For each segmented, I would create a new view controller subclass.

On your main view controller, for each segment, use a 'Container View' instead of a UIView object.

This will create two new 'screens' in the storyboard, attached to your main view controller with a segue. These new screens will be UIViewControllers, you can change them to be your subclass's as normal.

You can now use your prepareForSegue function as normal to set data in your segmented control view controllers.

Simon
  • 1,354
  • 1
  • 14
  • 18
1

So you have something like viewControllerMain, which contains viewSegmentedOne and viewSegmentedTwo, and you want to be able to access `viewControllerMain.myProperty' ?

You can always navigate through the hierarchy to get parent views - but the easiest option could be to include a reference to viewControllerMain in each of the segmented controls

var myParentVC : ViewControllerMain?

then when you create the subviews

mySubView.myParentVC = self
Russell
  • 5,436
  • 2
  • 19
  • 27
0

If you are using storyboard for view controllers, then try like this:

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Your_VC_Identifier");
viewController.Your_var = Your_value_to_assign

NOTE: Define Your_var in your ViewController class

Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
0

You just need to create an instance of the view controller you want to display, this is easy such as calling on the storyboard instance (usually the presenting view controller has one) instantiateViewController(withIdentifier:), by using an identifier that you provide in the storyboard file.
One created you can pass the data you want by casting it to your view controller class and present it as you prefer.

Andrea
  • 26,120
  • 10
  • 85
  • 131
0

One method would be using singleton class . https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift/ this is how you can make singleton class.

other method could be using nsuserdefaults.

You need to decide which approach is best according to your requirement.

va05
  • 327
  • 1
  • 15
0

try this:

let defaults = UserDefaults.standard()
defaults.set(yourdata, forKey: "someObject")
print(defaults.object(forKey: "someObject"))
ios
  • 31
  • 3
  • 2
    Welcome to stack overflow :-) Code-only answers aren’t useful for the community. Please look at [answer] – JimHawkins Feb 01 '17 at 13:58
0

You can try use Extensions for UIViewController

private var storedDataKey: UInt8 = 0

extension UIViewController {
    var storedViewControllerData: UIViewController? {
        get {
            return objc_getAssociatedObject(self, &storedDataKey) as? UIViewController
        }
        set(newValue) {
            objc_setAssociatedObject(self, &storedDataKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
    }
}

This very useful you can send data with chain like:

viewControllerB.storedViewControllerData = viewControllerA.storedViewControllerData

or

func viewDidLoad() {
   doSomething(self.storedViewControllerData)
}
Bogdan Evsenev
  • 851
  • 9
  • 16