Unexpectedly found nil while unwrapping an Optional value

Hi all,



This is my code:


import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!

@IBAction func saveButton(sender: UIBarButtonItem) {

println("The button was clicked")

self.dismissViewControllerAnimated(true, completion: nil)

PetTable.Profile1.textLabel!.text = "Jim"

}

@IBAction func cancelButton(sender: UIBarButtonItem) {

}

override func viewDidLoad() {

super.viewDidLoad()

/

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

func textFieldShouldReturn(textField: UITextField) -> Bool {

textField.resignFirstResponder()

return true;

}

}



var PetTable: PetTableViewController = PetTableViewController()


class PetTableViewController: UITableViewController{

@IBOutlet var Profile1: UITableViewCell!

@IBOutlet var Profile2: UITableViewCell!

@IBOutlet var Profile3: UITableViewCell!

@IBOutlet var Name1: UILabel!

override func viewDidLoad() {

super.viewDidLoad()

Name1.text? = "It Worked!"




I am trying to change a table view cell's label from another view when a bar button is clicked and I get an error saying: "fatal Error: Unexpectedly found nil while unwrapping an Optional value.

I really need help on this one as I have searched online for this topic and tried many things but nothing has worked. I would love to know if there is anything that can be done to solve this error or whether there is another war of changing a label from a different view.


Thanks,

James

Creating a new object instance with PetTableViewController() does not hook up any outlets. Views will only be created (and outlets hooked up) if you use UIKit to instantiate the view controller, such as with UIStoryboard's instantiateViewControllerWithIdentifier method, or via a storyboard segue. And even then the outlets will not be hooked up immediately; they will be hooked up at some later time when the view is loaded. You can't assume they are non-nil until viewDidLoad is called on the destination VC at the earliest.


Which brings us to the next point. One view controller should never reach into another view controller and try to modify its views directly. It's a violation of the MVC paradigm and leads to problems like this. You should define a public property on the "target" view controller that you will use to store the information to be displayed. The UI widget used to display it is an implementation detail that should not be exposed. Then in the target view controller's viewDidLoad, it should refer to that property and populate its UI. That way if you change the UI later (use a UITextField instead of a UILabel, for example, or switch to dynamic cells instead of static) the first VC doesn't need to know about it.

Junkpile,


Thank you for replying so soon.

I am very new to XCode and understand some of what you are saying. If it is not too much to ask could you explain how I would go about doing this?


Thanks again,

Iguana321

thanks a ton. This helped a lot

Unexpectedly found nil while unwrapping an Optional value
 
 
Q