Accessing objects instantiated through XIB file


#1

Hey everyone!

I went through the last challenge of the chapter and I still have this question:

So, I have an NSApplication object that is responsible for the app as a whole. During the loading of the app, NSApplication then calls up the main xib file of the project and sends an init message to all the objects declared in it, creating an instance of them.

Let’s take the official solution for the ToDoList challenge. In there I have an App Delegate, which is created by default, and I have a ToDo Controller, which I created myself. To create an instance of the To Do controller, I drag an NSObject from the Cocoa library to the dock in the Interface Builder. It creates an instance and the code works fine

My question is, how can I access the ToDo Controller object from other parts of the code? For example, let’s say I have a method in the ToDo Controller that I want to call from the App Delegate, is there anyway I can pass a message to the ToDo Controller from the App Delegate?

Maybe like

[[NSApplication ToDoController] doMyMethod]; ??

Thanks! :slight_smile:


#2

Rather than getting an object to invoke a method directly on another object, you can use the notification center to get one object to send a notification to another object to get something done. This approach results in less coupling between the objects involved.


#3

Thanks man, really appreciate the answer :slight_smile: