I am working on my first real Mac program, and I’m having troubling figuring out the right way to get my controller and UI to “see” my model. (Note: I am running Xcode 3.2.6 on OS X 10.6.8.)
Essentially, my program has a few text fields, a button, and a progress indicator. I made a class which acts as a controller, that subclasses NSObject (not NSController).
My controller has outlets for the UI elements, an instance variable for my model object, and the button sends an action to my controller. When the button is pressed, the controller reads the data from the text fields and tells the model object, “Here is the data, do your thing.” So far so good.
The model will take a long time to process the data, so I plan to have it make an NSOperation and put it in an NSOperationQueue. I think I can figure that part out though.
Here is what I need help with:
As the model does its work, it keeps keeps track of how far along it is. I want to show that in the progress bar. Numerically, this is simple. The model knows how many steps it will take, and which step it is on. But I do not understand what I have to do to make the progress bar display it.
I think I am supposed to use Cocoa Bindings, but I have only ever done that through a subclass of NSController before, and I am still quite shaky on setting it up. I could also use notifications, but that seems like a roundabout way to get the job done.
Also, my controller needs to know when the model finishes its work, so it can do things like update the UI, and allow the user to save the results. Same problem: I don’t know how to get my controller to know when the job is done. I suppose I could give the model object a “delegate” instance variable, set it as the controller, and just message it when the work is done, but that seems like it would violate MVC independence. I feel like my model object should not need to know anything about my controller.
So, what exactly is the correct/best solution for getting the UI and controller to know the state of the model?
Essentially, my program has a few text fields, a button, and a progress indicator. I made a class which acts as a controller, that subclasses NSObject (not NSController).
My controller has outlets for the UI elements, an instance variable for my model object, and the button sends an action to my controller. When the button is pressed, the controller reads the data from the text fields and tells the model object, “Here is the data, do your thing.” So far so good.
The model will take a long time to process the data, so I plan to have it make an NSOperation and put it in an NSOperationQueue. I think I can figure that part out though.
Here is what I need help with:
As the model does its work, it keeps keeps track of how far along it is. I want to show that in the progress bar. Numerically, this is simple. The model knows how many steps it will take, and which step it is on. But I do not understand what I have to do to make the progress bar display it.
I think I am supposed to use Cocoa Bindings, but I have only ever done that through a subclass of NSController before, and I am still quite shaky on setting it up. I could also use notifications, but that seems like a roundabout way to get the job done.
Also, my controller needs to know when the model finishes its work, so it can do things like update the UI, and allow the user to save the results. Same problem: I don’t know how to get my controller to know when the job is done. I suppose I could give the model object a “delegate” instance variable, set it as the controller, and just message it when the work is done, but that seems like it would violate MVC independence. I feel like my model object should not need to know anything about my controller.
So, what exactly is the correct/best solution for getting the UI and controller to know the state of the model?
Last edited: