-4

In the drawing I have drawn the menus according to the user from the database in the entry window. These inbound menus need to be sent in the Menu Monitoring controller section. There's SWReveal library in between.

How can I transfer this data?

Main storyboard image:

Main storyboard image

Bugs
  • 4,456
  • 9
  • 32
  • 40
  • you need to use `performSegue` Method to pass the data between different controllers. `segue.destination` property will give you the coming controller's object and you can pass data to it as per your requirement. – Priyal Jul 31 '17 at 08:56
  • 3
    Possible duplicate of [How do you share data between view controllers and other objects in Swift?](https://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift) – Priyal Jul 31 '17 at 08:57
  • @Priyal I did research before I asked this question. Since I can not reach a conclusion, I ask the question. I looked at the link you sent – Tuna Yangir Jul 31 '17 at 09:03
  • what have you tried and what's the problem? – Priyal Jul 31 '17 at 09:05

1 Answers1

0

Since you are using a storyboard, your first view controller can override prepareForSegue, which is exactly what it's there for. A UIStoryboardSegue object is passed in when this method is called, and it contains a reference to our destination view controller. Here, we can set the values we want to pass.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "MySegueID" {
        if let destination = segue.destinationViewController as? SecondController {
            destination.myInformation = self.myInformation
        }
    }
}

In your case since ATA Uni Obs is not the direct child of Login View Controller, hence you will first have to pass the information to Reveal View Controller and from there to ATA Uni Obs view controller.

myInformation is a property on your view controller holding whatever data needs to be passed from one view controller to the next. They obviously don't have to have the same name on each controller.

The other suggestion is to create a data container singleton: An object that gets created once and only once in the life of your application, and persists for the life of your app.

This approach is well suited for a situation when you have global app data that needs to be available/modifiable across different classes in your app.

Here is a great article about how to implement Singleton https://medium.com/@chan.henryk/singleton-in-swift-3-4383875a5d4d

The third and not so popular way is to save the data in UserDefaults. Mind that you need to understand what kind of that you can save into UserDefaults. Here you can read a bit more about UserDefaults: - http://www.ios-blog.co.uk/tutorials/objective-c/storing-data-with-nsuserdefaults/

This is how you can set the data in UserDefaults

let defaults = UserDefaults.standard
if defaults.string(forKey: "market") == nil{
    defaults.set("region", forKey: "market")

and to get the data from the UserDefaults you can do the following

region = defaults.string(forKey: "market")!            

Also please have a look at this question where it is explained in much more detail how to transfer data between multiple view controllers: - How do you share data between view controllers and other objects in Swift?

Phyber
  • 1,330
  • 9
  • 23
sarosh mirza
  • 717
  • 5
  • 12