Stuck (and frustrated) Newbie -- Archiving challenge


#1

I am working on completing the archiving challenge for TouchTracker. After reading the forum and reviewing the book, I understand that the code needs be reworked to fit a MVC model.

I have created a new viewController to hold the data sets (linesInProgress, completeLines). (so that meets the Model piece)

[code]#import “TouchTrackerAppDelegate.h”
#import “TouchTrackerViewController.h”
#import “TouchDrawView.h”

@implementation TouchTrackerAppDelegate

@synthesize window;

#pragma mark -
#pragma mark Application lifecycle

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    TouchTrackerViewController *touchTrackerViewController = [[TouchTrackerViewController alloc] initWithNibName: @“TouchTrackerViewController” bundle: nil];
    [window setRootViewController: touchTrackerViewController];

    [touchTrackerViewController setLinesInProcess: [NSMutableDictionary dictionary]];
    [touchTrackerViewController setCompleteLines: [NSMutableArray array]];

    [self.window makeKeyAndVisible];

    return YES;
    }[/code]

    I have set the view through IB to TouchDrawView by adding a view to the controller’s view and set the view to “TouchDrawView” from the drop down in the inspector to tell the compiler that this is the custom view that I want it to use. (That should meet the view piece).

In application:didFinishLaunching…:, I have instantiated the view controller and set it’s two attributes to an empty array and an empty dictionary. Which should instantiate instances of the array and dictionary to hold the data.

In the view, (TouchDrawView) i cannot get it to recognize the array and dictionary iVars. I have tried calling “completLines” and “linesInProgress” directly in the drawRect: method of the view, I have tried creating new iVars as attributes in the view and assigning them to the viewController’s attributes. I keep getting “undeclared variable” errors anytime that I refer to these to data pieces.

I figure that I need to understand this basic concept of passing information between classes completely before I move any further ahead.

Any advice?


#2

Hi,

From your description it sounds to me like you’ve pretty much grasped the concept i.e. declare ivars in the view and point them to the ivars in the viewController.

You can do this in the same way that your appDelegate is setting up the viewController.

Declare ivars (e.g. linesInView) in the TouchDrawView.h and @synthesize to create the setter and getter.
In the ViewDidLoad method of your viewController do something like:
[(TouchDrawView *)[self view] setLinesInView:linesInProcess];

In your drawRect you should then be able to refer to linesInView which is pointing to the same object as linesInProcess.

HTH
Gareth