Link controller items to AppDelegate?

This is really basic: I've got an IBOutlet in ViewController, but when I use it in AppDelegate, Xcode says "Use of unresolved identifier".


What else do I need to do?

Can you add any more context to what you are trying to do?


I'm no expert here as I'm struggeling on similar IB / code issues on storyboards, but I'll speculate here. If you are trying to do some init routine in AppDelegate which populates data to ViewController, you might be hitting a point in the app startup where view controller is not instantiated or ready for use. It might be better to do your initizalization in the view controller itself.


I sometimes find it useful to pupulate NSLog statements in various init, viewDidLoad, awakeFromNib, etc. routines just to check what order these things are being instianted in and using their object references to visually check in the console window if pointers are set and I'm really sending things to objects I intend.


I hope this helps,

It's the latest version of Xcode 8.3.2, so I presume the latest version of Swift.


It's the standard error that you get when you have an undefined thing, so in this case it's "Use of unresolved identifier myTextWindow".


At this stage, the app does very little: I'm just trying to get text out to NSScrollView.

Here's my code in AppDelegate:


func putText(words: String) {

let text = myTextWindow.documentView!.textStorage!

let attr = NSAttributedString(string: words)

text.appendAttributedString(attr)

}


and here's the bit in ViewController:


@IBOutlet weak var myTextWindow: NSScrollView!

Accepted Answer

The IBOutlet "myTextWindow" is defined in the ViewController. You can refer to it in the ViewController.m . The AppDelegate has no idea what it is because it is not defined in the AppDelegate. To reference it from teh AppDelegate you would need to establish a connection from the AppDelegate to the object defined in the ViewController. But you most likely don't want to do that - you most likely want to fiddle with "myTextWindow" only in the controller for your view - aka ViewController.


Your AppDelegate will load and run your ViewController. In your ViewController there is a method called viewDidLoad and viewWillAppear. Those methods can do things to your myTextWindow.

Thanks.

This is why I prefer scripting. 😝 I just can't get my head round all the delegates and controllers and outlets and what-have-you.

Link controller items to AppDelegate?
 
 
Q