Note that there are some explanatory texts on larger screens.

plurals
  1. POXcode - Is this MVC?
    primarykey
    data
    text
    <p>I'm a part-time notepad website coder taking steps into IOS apps. I got my first mac last week and have cobbled together a half working app. Now, convinced by this <a href="https://stackoverflow.com/a/2443020/781200">SO answer</a>, I am restarting trying to learn MVC. I am on a very steep learning curve so please bear with me.</p> <p>I have read up on MVC, separation of layers, three boxes and some arrows, I get it. However, translating the theory into the real world is frustrating. Most example apps I have looked at do not seem to use MVC. Even this <a href="http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html" rel="nofollow noreferrer">LazyTableImages example</a> from Apple seems to use <code>app delegate</code> as the model which confuses me. I have built on this excellently <a href="http://fromideatoapp.com/downloads/example/" rel="nofollow noreferrer">simple MVC example</a>.</p> <p>My app retrieves location markers from a web service. I have a tabbed application using ARC. One tab has a Mapkit map to display the markers.</p> <p>A simple class to hold a marker record:</p> <pre><code>@interface MarkerRecord : NSObject @property (strong, nonatomic) NSDecimalNumber *lat; @property (strong, nonatomic) NSDecimalNumber *lon; @property (strong, nonatomic) NSString *des; </code></pre> <p>A Model class, holding a collection of Marker classes, to serve both views. This is my Model class .h:</p> <pre><code>@interface MarkersModel : NSObject - (void)getMarkers; // Send HTTP GET to web service to retrieve JSON markers - (void)postMarker; // Send HTTP POST to web service to INSERT new marker record </code></pre> <p>And the implementation:</p> <pre><code>@interface MarkersModel () @property (strong, nonatomic) NSArray *data; @end @implementation MarkersModel @synthesize data; - (void)getMarkers { // Send HTTP GET to web service to retrieve JSON markers ... } - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects { // Thanks to Restkit data is an array of Marker classes data = objects; // Pass data to controller } </code></pre> <p>A mapViewController class .h:</p> <pre><code>#import &lt;MapKit/MapKit.h&gt; #import "MarkersModel.h" @interface MapViewController : UIViewController&lt;MKMapViewDelegate&gt; { MarkersModel *markersModel; } @property (nonatomic, strong) IBOutlet MKMapView *mapView; @end </code></pre> <p>And the implementation:</p> <pre><code>@interface MapViewController () @end @implementation MapViewController - (void)viewDidLoad { [super viewDidLoad]; // Create instance of markers model markersModel = [[MarkersModel alloc] init]; } -(void)viewWillAppear:(BOOL)animated { [markersModel getMarkers]; } - (void)putMarkersOnMap { // To be called by Markers Model ... } </code></pre> <p>Here's my main question:</p> <ul> <li>Is my code outline above (the beginnings of) a decent MVC implementation?</li> </ul> <p>If so:</p> <ul> <li>how do I get the Model to update the Controller ( i.e. how do I get MarkersModel.objectLoader function to call mapViewController.putMarkersOnMap)?</li> </ul> <p>I strongly suspect the answer is a custom delegate on the controller but I cannot for the life of me see how to implement it. I cannot relate any examples to my situation. I think I need something like this on the controller:</p> <pre><code>@class MapViewController; @protocol MapViewControllerDelegate; </code></pre> <p>While I am at it some additional questions:</p> <ul> <li>Will I create any dangling pointers - I suspect something should be <code>unsafe_unretained</code> (I am compiling to 4.3)?</li> <li>Is my MarkersModel declaration in the controller class correct? I am concerned that it will not be managed by ARC. I also suspect it is public but should be private.</li> <li>Should the MarkersModel.getMarkers method be static?</li> </ul> <p>I hope this is clear and an acceptable question. Thanks for your patience, Polly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload