Passing textField Data With a List between ViewControllers


#1

I have an app where an individual creates a profile via a textField. When save is pressed it creates a string in a data model with a List attached to it. On send it needs to pass data to the second viewController, so it can populate its List. Currently the textField is populating a comboBox, but the data is not being Passed. I researched for a few days now to no avail.

It is different from passing data with tableCell Views. Here is the code

First View Controller

import Cocoa
import RealmSwift

class SubmitViewController: NSViewController {

let realm = try! Realm()
var namesArray: Results<Name>?

@IBOutlet var textField: NSTextField!

override func viewDidLoad() {
    super.viewDidLoad()
    loadCategory()
    // Do view setup here.
}

func save(category: Name) {
    do {
        try realm.write {
            realm.add(category)
        }
    }  catch {
        print("There was an error saving context \(error)")
    }

}

func loadCategory() {
    namesArray = realm.objects(Name.self)
}

@IBAction func Save(_ sender: NSButton) {
    print("segue to submit")
    
    let newData = Name()
    newData.name = textField.stringValue
    
    self.save(category: newData)
    
    if let mainWC = view.window?.windowController as? MainWindowController {
        mainWC.segueToSubmit()
        print("segue to submit")
    }
}

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    
    let destinationVC = segue.destinationController as! SaveViewController
    
    if let profileName = namesArray?.index(of: Name) {
        destinationVC.selectedCategory = profileName
        
    }
    }
}

Second ViewController

import Cocoa
import RealmSwift

class SaveViewController: NSViewController {

let realm = try! Realm()
var detail : Results<Details>?
var nameArray : Results<Name>?

var selectedCategory : Name? {
    didSet{
        loadCategory()
    }
}

@IBOutlet var textFieldOne: NSTextField!
@IBOutlet var textFieldTwo: NSTextField!
@IBOutlet var comboBoxOne: NSComboBox!


override func viewDidLoad() {
    super.viewDidLoad()
    
    loadCategory()
    comboBoxOne.usesDataSource = true
    comboBoxOne.dataSource = self
    // Do any additional setup after loading the view.
    self.textFieldOne.stringValue = (self.detail?.last?.detailOne) ?? ""
    self.textFieldOne.stringValue = (self.detail?.last?.detailTwo) ?? ""
}

func loadCategory() {
     detail = selectedCategory?.names.sorted(byKeyPath: "name", ascending: true)
     nameArray = realm.objects(Name.self)
    comboBoxOne.reloadData()
}

@IBAction func addInfo(_ sender: NSButton) {
    print("segue to submit")
    if let mainWC = view.window?.windowController as? MainWindowController {
        mainWC.segueToHome()
        print("segue to submit")
    }
}

@IBAction func savePressed(_ sender: NSButton) {
    
    
    if let currentCategory = self.selectedCategory {
        do {
            try self.realm.write {
                let newItem = Details()
                newItem.detailOne = textFieldOne.stringValue
                newItem.detailTwo = textFieldTwo.stringValue
                currentCategory.names.append(newItem)
            }
        } catch {
            print("Error saving new items, \(error)")
        }
    }
    
}

}
extension SaveViewController: NSComboBoxDataSource {

func numberOfItems(in comboBox: NSComboBox) -> Int {
    return nameArray?.count ?? 0
    
}

func comboBox(_ comboBox: NSComboBox, objectValueForItemAt index: Int) -> Any? {
    print(nameArray?[index] as Any)
    return nameArray?[index].name
    }

}


#2

The code is a little confusing as loadCategory is being called when selectedCategory is set but then also being called on viewDidLoad().

It’s also a little confusing that selectedCategory is being set to a single profileName upon creating of the destination viewController

if let profileName = namesArray?.index(of: Name) {
    destinationVC.selectedCategory = profileName

but then the selectedCategory has as a names property?

detail = selectedCategory?.names.

Also, ‘detail’ doesn’t appear to be used anywhere as your combobox is pulling it’s data from the nameArray

func numberOfItems(in comboBox: NSComboBox) -> Int {
return nameArray?.count ?? 0

which contains all of the Name objects

nameArray = realm.objects(Name.self)

Can you clarify the question? what specifically isn’t being passed and how do you know since the selectedCategory isn’t being used as a datasource.