2

It's my first time creating an app with Xcode, and I have come across an error I can't figure out. The fatal error:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value, occurs on line 29.

I've seen other posts with comments saying "check your outlets", but they're all good. I've included the Connections inspector of one of my textfields in case it helps.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var mssgArray:[Int] = [0,0,0,0,0,0,0,0]
    
    @IBOutlet weak var ItemQ: UITextView!
    @IBOutlet weak var RTC: UITextView!
    @IBOutlet weak var ExpoPrompt: UITextView!
    @IBOutlet weak var OrderPrompt: UITextView!
    @IBOutlet weak var Data: UITextView!
    
    @IBOutlet weak var CusID: UITextField!
    @IBOutlet weak var FirstN: UITextField!
    @IBOutlet weak var LastN: UITextField!
    @IBOutlet weak var PhoneNum: UITextField!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CusID.delegate = self
        FirstN.delegate = self
        LastN.delegate = self
        PhoneNum.delegate = self
    }
    
    @IBAction func PizzaBut(_ sender: UIButton) {
        if(mssgArray[0] == 0)
        {
            ItemQ.text += "Pizza\n"
            mssgArray[0] = 1
        }
        if(mssgArray[0] == 1)
        {
            mssgArray[0] = 2
        }
    }
    @IBAction func SubBut(_ sender: UIButton) {
        if(mssgArray[1] == 0)
        {
            ItemQ.text += "Sub\n"
            mssgArray[1] = 1
        }
        if(mssgArray[1] == 1)
        {
            mssgArray[1] = 2
        }
    }
    @IBAction func QuesoBut(_ sender: UIButton) {
        if(mssgArray[2] == 0)
        {
            ItemQ.text += "Queso\n"
            mssgArray[2] = 1
        }
        if(mssgArray[2] == 1)
        {
            mssgArray[2] = 2
        }
    }
    @IBAction func BagBut(_ sender: UIButton) {
        if(mssgArray[3] == 0)
        {
            ItemQ.text += "Baguette\n"
            mssgArray[3] = 1
        }
        if(mssgArray[3] == 1)
        {
            mssgArray[3] = 2
        }
    }
    @IBAction func PretzelBut(_ sender: UIButton) {
        if(mssgArray[4] == 0)
        {
            ItemQ.text += "Pretzels\n"
            mssgArray[4] = 1
        }
    }
    @IBAction func MeatballBut(_ sender: UIButton) {
        if(mssgArray[5] == 0)
        {
            ItemQ.text += "Meatball\n"
            mssgArray[5] = 1
        }
        if(mssgArray[5] == 1)
        {
            mssgArray[5] = 2
        }
    }
    @IBAction func BoardBut(_ sender: UIButton) {
        if(mssgArray[6] == 0)
        {
            ItemQ.text += "Charcuterie Board\n"
            mssgArray[0] = 1
        }
        if(mssgArray[6] == 1)
        {
            mssgArray[6] = 2
        }
    }
    @IBAction func SaladBut(_ sender: UIButton) {
        if(mssgArray[7] == 0)
        {
            ItemQ.text += "Salad\n"
            mssgArray[7] = 1
        }
        if(mssgArray[7] == 1)
        {
            mssgArray[7] = 2
        }
    }
    @IBAction func ClearBut(_ sender: UIButton) {
        ItemQ.text = ""
        mssgArray = [0,0,0,0,0,0,0,0]
    }
    @IBAction func SendMssg(_ sender: UIButton) {
        ItemQ.text = ""
        mssgArray = [0,0,0,0,0,0,0,0]
    }
    @IBAction func AddCus(_ sender: UIButton) {
    }
    @IBAction func SearchCus(_ sender: UIButton) {
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if(textField == CusID || textField == PhoneNum)
        {
            textField.keyboardType = UIKeyboardType.phonePad
        }
    }
}

Connections Inspector Screenshot

And here is a screenshot of the two tab views and what I'm seeing, the right tab is showing the tab bar controller's controller inspector.

Controller Inspector Screenshot

Nat Riddle
  • 928
  • 1
  • 10
  • 24
Patrick
  • 41
  • 6
  • Is it connected to the right view controller? It says "Expo" in the screenshot that you posted – aheze Jun 09 '21 at 22:32
  • I am using a tab bar controller for two tab views. Both are connected to the same view controller. The buttons on each view work so I believe they are connected properly. Am I misunderstanding? If so where do I check what your talking about? – Patrick Jun 09 '21 at 22:54
  • 2
    First, which line is line 29? Second, what is “Expo”? That is not included in your screen snippet. I would be inclined to (a) delete that outlet that has been hooked up in IB; (b) double check the base class for your scene in IB; and (c) hook up that outlet again. Where it says “CusID -- Expo” in IB outlets inspector, it generally would say “CusID -- View Controller”. The name on the right side is generally the class to which it has been hooked up. But “Expo” doesn't make sense given your code snippet. – Rob Jun 10 '21 at 00:23
  • 2
    (When you get your current problem behind you, you may want to use “Editor” » “Refactor” » “Rename...” to rename those outlets because as a matter of convention, (a) they should start with a lower case letter; and (b) they generally have a name that describes what they are. E.g., rather than `CusID` you would have something like `customerIdTextField`. Don't try to start renaming, though, until you have your current problem behind you, because you want to work on one thing at a time.) – Rob Jun 10 '21 at 00:26
  • Expo is the name of one of the tab views sorry. Line 29 is: CusID.delegate = self – Patrick Jun 10 '21 at 01:43
  • I added a new pic above. What do you mean by "in IB". Ive tried many times to delete and re hookup my outlets but Expo is always what its hooked up to. I appreciate the help. – Patrick Jun 10 '21 at 02:03
  • 1
    When I say “in IB”, I’m referring to “Interface Builder”, the UI in which you edit your storyboards. (It’s the “IB” in `@IBOutlet`. The term is a bit of a through-back where we used to edit UI in a separate app called IB.) So, you’re showing us screen snapshots where the tab’s view controller is `Expo` and that the outlet inspector is confirming that, too. But you’re reporting that the `@IBOutlet` in `ViewController` (in the code snippet) is `nil`, which makes sense, because the outlet is in `Expo`, not `ViewController`. So, an @IBOutlet` in `ViewController`, a different class, should be nil… – Rob Jun 10 '21 at 13:38
  • You pointed me in the right direction and I figured it out. I had both views pointing to the same view controller instead of their own. I restarted from scratch and reached the same point while following the conventions you listed. The delegate error is gone. Thank you! – Patrick Jun 10 '21 at 16:25

0 Answers0