Dear James, I think you would like to take a closer look at the Model-View-Controller paradigm. In your application you are trying to implement some kind of "superclass". Let me explain what this means:
In your MainViewController class, which is clearly the controller, there is also part of the implemented model. This is a bad idea, but very common at the beginning. Perhaps I misunderstood your design, but here's how I implement it:
Model . I would use a suitable model object, which in your case can be as simple as a custom subclass of NSObject with NSMutableArray as a property. In addition, this model will have methods for extracting data from the Internet. That's right: create networks in the model. You will need to have methods like - (void) refreshProductCode that you could call from your controller. If you want real fantasy, use NSOperation to encapsulate the load (then you will use the synchronous NSURLConnection , because the operation itself is already running asynchronously). It would be nice if your parsing JSON string takes longer, it also runs in the background, and your user interface remains responsive.
So, now the model is loading your stuff - fine, but how do I know when it will be done? Well, you will post a notification from the model as soon as it is done. What if the download failed? You guessed it was right: post a notification that it failed.
Controller The controller that must display data from the model must first obtain the model object. In this case, the model object is a property of your AppController. Then, the controller has a property for this model object and saves it, so that the model object does not disappear during controller operation. The controller then logs notifications for the model. So how will a regular download work?
- Get an instance of a model object
- call
-(void) refreshProductCode of the model object - display network activity in the status bar and wait for notifications
- when a notification appears, when the user interface is updated successfully and when the reboot is restarted, download or show the note to the user. Also disable the network activity counter.
How do you move data between view managers? . View controllers should work a bit like a mafia: each view controller works on the need to know. For example, if you want the view controller to display the details of your product, you would not transfer the model with all your products to the controller. Instead, you will have an instance variable on the detail view controller containing only one product model object that has all the information, such as description text, photos, etc. Then the cool thing is, if you ever want to display product information again in your application, you can reuse this view controller since all it needs is a product model object to do its job.
Gorilla patch
source share