title: How to pass Item from initial (one time) first time view controller to main view controller and save that data using core data
body: <p>I have been working on this issue for two days now and sadly I cannot figure out the issue to my problem. I'm trying to take one item from my initial one time view controller and send that to my main view controller where it will be saved within the main view controller and will appear upon that controller when reloading the app.</p>
  
  <p>Here is my app delegate code for the "first time" view controller</p>
  
  <pre><code> func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {
  
      if UserDefaults.standard.bool(forKey: "firstTimer") {
      let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
      let mainView = storyBoard.instantiateViewController(withIdentifier: "MainViewControllerID")
          let nav = UINavigationController(rootViewController: mainView)
          nav.navigationBar.isHidden = true
          self.window?.rootViewController = nav
      }
      return true
  }
  </code></pre>
  
  <p>containers and saveContext are default</p>
  
  <pre><code>import UIKit
  
  import CoreData
  
  class FirstTimeViewController: UIViewController {
  
  let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
   private var appDelegate = UIApplication.shared.delegate as! AppDelegate
  private var player = [Player]()
  
  
  
  
  
  @IBOutlet weak var nameTextField: UITextField!
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      // Do any additional setup after loading the view.
  }
  
  // This View Controller will only be used once upon the first time the app is being used.
  
  // MARK: Make func that prepares for segue on initial opening of app
  
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "toMainViewController" {
          let mainViewController = segue.destination as! UINavigationController
          let destination = mainViewController.topViewController as! MainViewController
          if let newPlayer = self.nameTextField.text{
              destination.name.name = newPlayer
              destination.playerData.name = newPlayer
              saveItems()
          }
      }
  
  }
  
  @IBAction func continueButtonPressed(_ sender: UIStoryboardSegue) {
      UserDefaults.standard.set(true, forKey: "firstTimer")
      let mainPlayer = PlayerData()
      let player1 = Player(entity: Player.entity(), insertInto: context)
      player1.name = mainPlayer.name
      performSegue(withIdentifier: "toMainViewController", sender: self)
      saveItems()
  }
  
  
  func saveItems() {
      do {
      try context.save()
          print("File Successfully saved!")
      }catch {
         print("Error saving Context \(error)")
      }
  }
  
  
  
  // MARK: Function to Save and Load data??
  /*
  // MARK: - Navigation
  
  // In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      // Get the new view controller using segue.destination.
      // Pass the selected object to the new view controller.
  }
  */
  
  func loadItems() {
      let request = Player.fetchRequest() as NSFetchRequest&lt;Player&gt;
      do {
          player = try context.fetch(request)
          print("Info loaded")
      } catch {
          print("Error fetching data from context \(error)")
      }
  }
  }
  </code></pre>
  
  <p>MainViewController being sent the information. I only want to send one item and save it to that main view controller.</p>
  
  <pre><code>import UIKit
  import Foundation
  import CoreData
  
  class MainViewController: UIViewController {
  //set up model object, buttons, and labels
  //    let player: Player!
  let context = (UIApplication.shared.delegate as!          AppDelegate).persistentContainer.viewContext
  private var appDelegate = UIApplication.shared.delegate as! AppDelegate
  //    lazy var nameText = Player(context: context)
  //    var playerInfo = [Player]()
  lazy var player = [Player]()
  let playerData = PlayerData()
  var name = ""
  
  
  @IBOutlet weak var playerName: UILabel!
  
  @IBOutlet weak var currentLevel: UILabel!
  
  @IBOutlet weak var xpCounter: UILabel!
  
  @IBOutlet weak var playerProfileImage: UIImageView!
  
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  //        loadItems()
  //        name = playerData.name
      if let nameOfPlayer = name.name {
          print("This is what we see: \(nameOfPlayer)")
      playerName.text = nameOfPlayer
      }
      appDelegate.saveContext()
  
  }
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
  //        loadView()
  
  }
  
  @IBAction func menuButtonPressed(_ sender: Any) {
  
  }
  
  func loadItems() {
      let request = Player.fetchRequest() as NSFetchRequest&lt;Player&gt;
      do {
          player = try context.fetch(request)
          print("Info loaded")
      } catch {
          print("Error fetching data from context \(error)")
      }
  }
  // MARK : Add Name to Main View
  
  
  // MARK : Add Xp To Main View
  
  // MARK : Add UI Image to profile image view
  
  // MARK:  (Optional) Create a 'Choose a task button to segue to the task tab'
  
  
  // MARK: Program the Progress Bar to update on xp gained and reset on level up
  
  
  // MARK: Function to Save and Load data??
  }
  </code></pre>
  
  <p>If dataSource code needed I will add upon request.</p>
  
  <p>Thanks!</p>