Is Xcode MVC?

I work on a notepad website part-time, following the steps in iOS apps. Last week I got my first Mac and put together half the working application. Now, having convinced myself of this SO answer , I restart the attempt to learn MVC. I am on a very steep learning curve, so please bear with me.

I read MVC, separating the layers, three boxes and a few arrows, I understood. However, translating theory into the real world is frustrating. Most of the applications I've been looking at don't seem to use MVC. Even this Apple LazyTableImages example seems to use app delegateas a model that bothers me. I built on this a perfectly simple MVC example .

My application retrieves location markers from a web service. I have a tabbed application using ARC. On one tab, there is a Mapkit map for displaying markers.

A simple class for storing marker entries:

@interface MarkerRecord : NSObject

@property (strong, nonatomic) NSDecimalNumber *lat;
@property (strong, nonatomic) NSDecimalNumber *lon;
@property (strong, nonatomic) NSString *des;

A model class containing a set of marker classes for serving both views. This is my Model.h class:

@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

And implementation:

@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

}

Class mapViewController.h:

#import <MapKit/MapKit.h>
#import "MarkersModel.h"

@interface MapViewController : UIViewController<MKMapViewDelegate> {
    MarkersModel *markersModel;
}

@property (nonatomic, strong) IBOutlet MKMapView *mapView;

@end

And implementation:

@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 
    ...
}

Here is my main question:

  • Is my code above (start) worthy of an MVC implementation?

If yes:

  • How to get the model for updating the controller (for example, how do I get the MarkersModel.objectLoader function to call mapViewController.putMarkersOnMap)?

I strongly suspect that the answer is a user delegate on the controller, but I can’t see for life how to implement it. I cannot relate any examples to my situation. I think I need something like this on the controller:

@class MapViewController;
@protocol MapViewControllerDelegate;

:

  • - - , - unsafe_unretained ( 4.3)?
  • MarkersModel ? , ARC . , , .
  • MarkersModel.getMarkers ?

, . , .

+5
1

() MVC?

, . , . MarkersModel:

@protocol MarkersModelDelegate<NSObject>
-(void) markersDidUpdate:(MarkersModel*)model;
@end

@interface MarkersModel : NSObject
{
    __weak id<MarkersModelDelegate> delegate;
}

@property(weak,nonatomic) id<MarkersModelDelegate> delegate;

:

@interface MapViewController : UIViewController<MKMapViewDelegate, MarkersModelDelegate> {

:

@implementation MarkersModel

@synthesize delegate;

//...

-(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
    // Thanks to Restkit data is an array of Marker classes
    data = objects;

    // Pass data to controller
    if( delegate != nil )
    {
        [delegate markersDidUpdate:self];
    }
}

- - , - ( 4.3)?

,

MarkersModel ? , ARC . , , .

, @private:

@interface MapViewController...
{
@private
MarkersModel *markersModel;
...

MarkersModel.getMarkers ?

, . MarkersModel , , . MarkersModel Markers, ,

+5

All Articles