Using Apple's rather old Swift Getting Started tutorial as a base, I've a working app. I'd like to add a UIView at the top of a table, to appear temporarily if an error occurs. Unfortunately, when trying to access UI elements from the UIView's associated class, I get an "Unexpectedly found nil while implicitly unwrapping an Optional value" error.
https://i.stack.imgur.com/h4hlj.png
This is actually a UIContainerView, but apparently appears as a UIView.
I've subclassed both the UIView and the linked view (via an embed segue) with a subclass I called ErrorView, and added an @IBOutlet link to the UIView in the table view's subclass. The UI elements are linked to the ErrorView class with an @IBOutlet.
When accessing ErrorView class members via the @IBOutlet in the table subclass, everything is dandy until the ErrorView class tries to access its @IBOutlet linked elements, at which point it crashes with "Unexpectedly found nil while implicitly unwrapping an Optional value."
Strangely, accessing the view itself from within the Error Class is possible, and as such I'm able to do something view-related like change its colour without an error - though the colour change doesn't actually happen.
A very trimmed down version of the code is below, which should reproduce the issue.
I'm using iOS13 and Xcode 11 beta.
ErrorView.swift
import UIKit
class ErrorView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
@IBOutlet weak var uiMessage: UILabel!
func setErrorMessage(errorMessage: String){
uiMessage.text=errorMessage //<-- error here
}
}
ModuleTableViewController
import UIKit
import Foundation
class ModuleTableViewController: UITableViewController {
@IBOutlet weak var errorView: ErrorView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
errorView.setErrorMessage(errorMessage: "Hello")
}
}