简体   繁体   中英

Realm data is saved to database but is not populating labels in Swift 4

I am making an app that uses Realm to persist data, and I have run into a small bug and cant seem to figure it out. I know it has to be something simple that I am overlooking, as I have a couple other View Controllers that use very similar code and they work. so here it is.

I have a View Controller that has inputs for information and it works fine sends the data to Realm. On a different View Controller I have code to display that data in labels but it does not display the information on the labels they are blank in fact even the default text does not show on the screen? Help!!!

Here is the code for the display VC.

import UIKit
import RealmSwift

class CompanyViewController: UIViewController {

    var myCompany: Results<CompanyInfo>?

    let realm = try! Realm()

    var selectedCompany: CompanyInfo? {
        didSet { }
    }

    override func viewWillAppear(_ animated: Bool) {
        loadCompany()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        nameLabel?.text = selectedCompany?.companyName
        addressLabel.text = selectedCompany?.companyAddress
        cityLabel.text = selectedCompany?.companyCity
        stateLabel.text = selectedCompany?.companyState
        postalLabel.text = selectedCompany?.companyPostalCode
        phoneLabel.text = selectedCompany?.companyPhoneNumber
        emailLabel.text = selectedCompany?.companyEmail
    }

    func loadCompany(){
        myCompany = realm.objects(CompanyInfo.self)
    }

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var cityLabel: UILabel!
    @IBOutlet weak var stateLabel: UILabel!
    @IBOutlet weak var postalLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "AddCompanySegue" {
            guard segue.destination is UINavigationController else { return }
        }
    }
}

You should call loadCompany method before set the labels text values, something like this

override func viewDidLoad() {
        super.viewDidLoad()
        //first load the company
        self.loadCompany()

        nameLabel?.text = selectedCompany?.companyName
        addressLabel.text = selectedCompany?.companyAddress
        cityLabel.text = selectedCompany?.companyCity
        stateLabel.text = selectedCompany?.companyState
        postalLabel.text = selectedCompany?.companyPostalCode
        phoneLabel.text = selectedCompany?.companyPhoneNumber
        emailLabel.text = selectedCompany?.companyEmail
    }

other issue is what is selectedCompany? assuming that is the first result of your realm query, your loadCompany method should be something like this

  func loadCompany(){
        myCompany = realm.objects(CompanyInfo.self)
        if(myCompany.count > 0){
        //Asigning value to "selectedCompany" variable
        self.selectedCompany = myCompany[0]
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM