0

I'm having a menuViewController and a ContentViewController using https://github.com/romaonthego/RESideMenu. The MenuViewController contain a list of different leagues retrieved from local database. This contain a league object with following vars leagueId and name. When a league is selected it should send the data to the ContentViewController. However the problem is that the MenuViewController is not presenting the viewController it is just hiding the menuViwController and therefore i can't pass data from menuViewController to contentViewController when a cell with a league is selected. i've therefore tried to save the leagueId to a NSUserDefault key, however this creates a problem when the app is exited, since it wont reset the NSUserDefaults. What is the best approach for such issue? should i rethink it?

pressing cell in menuViewController

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    NSUserDefaults.standardUserDefaults().setInteger(menuArray![indexPath.row].id, forKey: "leagueId")


    self.sideMenuViewController.hideMenuViewController()

}
Peter Pik
  • 11,023
  • 19
  • 84
  • 142
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – MQLN Nov 17 '15 at 23:38
  • did you even read the question @MacLean? this is not between two viewcontrollers – Peter Pik Nov 18 '15 at 17:23

1 Answers1

0

You can achieve this by creating a global class, First create a global class, :-

  import Foundation


     class User {
      class var sharedInstance: User {
      struct Static {
           static var instance: User?
           static var token: dispatch_once_t = 0
      }

     dispatch_once(&Static.token) {
        Static.instance = User()
     }

     return Static.instance!

     }

    var leagueId: Int?
    var name:String?
  }

then store data in the class that you want

   class ViewController: UIViewController { 

    let user = User.sharedInstance


     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

       if segue.identifier == "showDetails" {
            self.user.leagueId = Id
            self.user.name = Name
       } 
     }
   }

Then retrieve data :-

class ViewController: UIViewController { 

        let user = User.sharedInstance
        mylabel.text = self.user.leagueId 
        mylabael2.text = self.user.name

}

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58