Connect Outlets to Tab View Controller Result in Nil Error

42 views Asked by At

I've created a new window controller that hosts a tabViewController inside my app. I've added classes to the window controller, and have the same class across all the view controllers in the tab view.

I can connect buttons and give them an action and it works perfectly, however, I try to connect an outlet and attempt to change something via the outlet, it returns nil and crashes the program. For example, this exact code works in the pre-made viewController of my app, but returns:

Unexpectedly found nil while implicitly unwrapping an Optional value 

when running it through the new tab view controller I created.

What's weird to me is I can use a regular view controller and the outlets connect fine, but if I want to use a tab view controller, these nil errors are happening.

I made sure that the nil was not related to grabbing the inputs for the button by printing the audio devices, and the audio devices are there and able to be printed. It seems as if the button is not there even though it is connected.

I have also tried to simply change an NSTextField color after connecting it to an outlet and this returns the same nil error.

Any idea what I might be doing wrong here? Thanks so much for the help.

class FirstLaunchViewController: NSViewController {
  
    var FLWindow: FirstLaunchWindowController?
    var selectedAudioDevice = [String]()        
    @IBOutlet weak var deviceListPopUpButton: NSPopUpButton!        
    @IBOutlet weak var visualizeButton: NSButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height)
        populateDeviceList()
 
    }
    @IBAction func CloseStartupGuide(_ sender: Any) {
        self.view.window?.windowController?.close()
    }
    
    @IBAction func popUpDidChange(_ sender: Any) {
        print("changed")
    }
    
    //grab inputs for button
    fileprivate func populateDeviceList() {

        deviceListPopUpButton.removeAllItems()
        for device in AudioDevice.allInputDevices() {
            var teststring = ""
            teststring = device.uid!
            print(device.name)
            deviceListPopUpButton.addItem(withTitle: device.name)
            deviceListPopUpButton.lastItem?.tag = Int(device.id)
            selectedAudioDevice.append(device.uid!)
                
            }
        }
    }    
}
0

There are 0 answers