Search code examples
iosswiftxcodexib

Error "not coding-compliant" for XIB VC IBOutlet's


*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x101664480> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'

Why do I get this error when I connect an IBOutlet like so;
@IBOutlet weak var label: UILabel!

func viewDidLoad() { super.viewDidLoad() label.text = "Hello World!" }

The xib's file's owner is set to the custom UIViewController class

Is it something special for UIViewController + xib ?

Connected IBOutlet

Ignore import NightNight

Outlets

The VC is loaded via this func in a UITableView;

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let topicVCName = UIViewController(nibName: categories[indexPath.section].refName[indexPath.row], bundle: nil) navigationController?.pushViewController(topicVCName, animated: true) }
If I take away the outlets the xib loads fine with no errors. (Obviously)


Solution

  • The answer lies in the error message:

    UIViewController 0x101664480
    

    This proves that your view controller in the storyboard (or wherever the view controller itself comes from) is a plain vanilla UIViewController and not a SingleNumberVC. It is true that SingleNumberVC has a label property, but UIViewController does not!

    You have said:

    xib's file's owner is set to the custom UIViewController class

    Indeed, because otherwise you could not have formed the outlet at all. But when the code runs, the view controller itself is not the custom UIViewController class. You did not answer the question of where it comes from, but I assure you, wherever that is, you will find you have forgotten to specify SingleNumberVC.

    EDIT Oh, I see, here it is:

    let topicVCName = UIViewController(nibName: categories[indexPath.section].refName[indexPath.row], bundle: nil)
    

    Change UIViewController to SingleNumberVC in that line.