0

I have 2 ViewControllers and when I am in the second Viewcontroller I want to reach an IBOutlet in first ViewController. Although I instantiate I am always getting Fatal Error : "unexpectedly found nil while unwrapping an Optional value"

Here is my code

@IBAction func deleteCompanyPressed(_ sender: AnyObject) {

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "ReceivedChequeDetailVCID") as! ReceivedChequeDetailVC
    vc.receivedCompanyOutlet.tintColor = UIColor.darkGray
    vc.receivedCompanyOutlet.setTitle("Çekin Alındığı Firma / Kişi", for: .normal)
}

Error happens when it is trying to change the IBOutlet

  • check your ViewControllerIdentifier if it's the same as in storyBoard, and try to not use force unwraps. – Radu Nunu Oct 31 '16 at 14:35
  • I checked, it is the exact same name. What do you mean by not using unwrapping? @RaduNunu – Burak Akdeniz Oct 31 '16 at 14:52
  • Than i guess your storyboard it's null, try to init with storyboard name like this UIStoryboard(name: "YOUR_STORYBOARD_NAME", bundle: NSBundle.mainBundle()). About unwrapping, i mean to not use ! but if let or guard statements to get the values – Radu Nunu Oct 31 '16 at 15:20
  • When you first instantiate a view controller, its outlets are not yet hooked up. That won't happen until later in the process (e.g. you present that view controller and `viewDidLoad` is called). So, don't try to access outlets immediately. Create string properties to pass the data, and then have `viewDidLoad` update the controls using those `String` values. E.g. http://stackoverflow.com/a/29865925/1271826 – Rob Oct 31 '16 at 16:20

2 Answers2

-1

You problem can come from 2 point in your code: - either self.storyboard is not instantiated so it is nil - either vc is not of type ReceivedChequeDetailVC

My solution is the following:

@IBAction func deleteCompanyPressed(_ sender: AnyObject) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ReceivedChequeDetailVCID") as? ReceivedChequeDetailVC
vc?.receivedCompanyOutlet.tintColor = UIColor.darkGray
vc?.receivedCompanyOutlet.setTitle("Çekin Alındığı Firma / Kişi", for: .normal)}
Ionut
  • 374
  • 4
  • 10
  • I tried the solution, it doesnt seem that working. I still cant reach the IBOutlet. There is gotta be smth else :( @Ionut – Burak Akdeniz Oct 31 '16 at 14:51
-1

As others have suggested; your force cast (as!) may be hiding a failure to obtain a ReceivedChequeDetailVC.

Additionally (and this is impossible to be certain of since you did not provide your ReceivedChequeDetailVC implementation or indicate which line is throwing your exception), if receivedCompanyOutlet is a force unwrapped property it may be the source of your problem. It sounds like receivedCompanyOutlet is a subview of your ReceivedChequeDetailVC's view. You may be successfully creating the view controller but never loading its view and then attempting to access a force unwrapped property which has not yet been set. See the documentation for view and loadView.

Jonah
  • 17,918
  • 1
  • 43
  • 70