Search code examples
iosswiftxcodeseguelabel

Swift 3.0 Labels Coming Blank (Nil value?)


I'm quite new Swift, and I'm trying to make an app that allows the user to input event information into text fields, then when they press a button that info is displayed in labels in a separate view. However, I am currently having an issue in that when I click the button in the simulator, having entered info into each field, I just get a completely blank view instead of the labels displaying the info.

Here is the code for the controller where the information is inputted:

import UIKit

class CreateEventController: UIViewController {

    @IBOutlet weak var TitleTxt: UITextField!
    @IBOutlet weak var CityTxt: UITextField!
    @IBOutlet weak var CountryTxt: UITextField!
    @IBOutlet weak var VenueTxt: UITextField!
    @IBOutlet weak var TimeTxt: UITextField!
    @IBOutlet weak var UrlTxt: UITextField!

    @IBAction func SaveEventBtn(_ sender: UIButton) {

        if TitleTxt.text!.isEmpty || CityTxt.text!.isEmpty || 
    CountryTxt.text!.isEmpty || VenueTxt.text!.isEmpty ||
        TimeTxt.text!.isEmpty || UrlTxt.text!.isEmpty{

            let alert = UIAlertController(title: "Missing values", message: "Please populate all fields", preferredStyle: UIAlertControllerStyle.alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

            self.present(alert, animated: true, completion: nil)

        }

        else {

            print("The event title is \((TitleTxt.text)!)")

            func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) 
            {
                if (segue.identifier == "CreateEvntBtn") {
                    let destVC = segue.destination as! CreatedEventController

                    destVC.CrTitle = TitleTxt.text!
                    destVC.CrCity = CityTxt.text!
                    destVC.CrCountry = CountryTxt.text!
                    destVC.CrVenue = VenueTxt.text!
                    destVC.CrTime = TimeTxt.text!
                    destVC.CrUrl = UrlTxt.text!

                }
            }

        }

    }

}

And here is the view controller where the info is supposed to be displayed:

import UIKit

class CreatedEventController: UIViewController {


    @IBOutlet weak var CrTitleLbl: UILabel!
    @IBOutlet weak var CrCityLbl: UILabel!
    @IBOutlet weak var CrCountryLbl: UILabel!
    @IBOutlet weak var CrVenueLbl: UILabel!
    @IBOutlet weak var CrTimeLbl: UILabel!
    @IBOutlet weak var CrUrlLbl: UILabel!

    var CrTitle, CrCity, CrCountry, CrVenue, CrTime, CrUrl : String!

    override func viewDidLoad() {
        super.viewDidLoad()

        CrTitleLbl.text = CrTitle
        CrCityLbl.text = CrCity
        CrCountryLbl.text = CrCountry
        CrVenueLbl.text = CrVenue
        CrTimeLbl.text = CrTime
        CrUrlLbl.text = CrUrl
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


}

I'm sure I'm missing something really simple, but any help would be appreciated. Cheers!


Solution

  • Your prepareForSegue function is nested inside another function. It needs to be at the level of the class. Move it outside of SaveEventBtn.

    Additionally, you need to actually perform the segue. Where you currently have prepareForSegue, add performSegue(withIdentifier: "mySegueID", sender: nil). Note that mySegueID is the name of the segue you create in your storyboard. You currently have it the same as the button action, which might be the case, but you should probably call it something else.

    Also, to make your code more readable in the future and save yourself headaches from mistakes, Swift's standard is to make variable names (TitleTxt, CountryTxt) and function names (SaveEventBtn) camel case (titleTextField, countryTextField, saveTapped).