Communication between classes
Communication between classes
- Subject: Communication between classes
- From: Jeremy Dronfield <email@hidden>
- Date: Thu, 27 Jun 2002 17:52:14 +0100
I've got a very basic (and to most people probably very dopy) question.
I've built an application for browsing text files. I built a perfectly
good prototype with everything in a single window and all the code in a
single window controller. I now want to bring it into line with the MVC
paradigm by having (initially) two windows (a controls window and a text
viewing window), with data handling done separately (by an NSObject
subclass). I've adapted my code accordingly, but I can't get the thing
to work. I'm not getting any build errors, nor any runtime errors, nor
does anything show up in debugging. The application just sits there,
dead. I've inserted little monitoring features (text fields to display
instance variable values, which return as 0 then die, and a progress
indicator which doesn't respond on the first button click, then fills
right up on the second), and the problem is with passing values back to
the window controller subclass. Communication takes place, but the
variables are somehow stripped of their values en route. I'm not at all
confident that I'm initializing my classes properly. I've combed through
the object class header files, the NSObject and application architecure
documentation until my frontal lobes throb, and can't get anywhere in
the morass of vague (often conflicting) guidance. My latest version
includes an NSDocument subclass (which I'm not even sure I need), and
goes like this (I've left out most of the accessors, since there doesn't
seem to be any problem with them - also, there's nothing here about the
display window, since I haven't even begun to implement it):
#import <Cocoa/Cocoa.h>
@class SRSettings;
@interface SRControls : NSWindowController
{
IBOutlet id backButton;
IBOutlet id markButton;
IBOutlet id nextButton;
IBOutlet id progressBar;
IBOutlet id display;
IBOutlet id display2;
SRSettings *sRSettings;
}
// Accessors
- (SRSettings *)sRSettings;
@end
-------------------------------------------------------
#import "SRControls.h"
#import "SRSettings.h"
@implementation SRControls
- (id)init
{
if (self = [super initWithWindowNibName:@"MainMenu.nib"]) {
[self setShouldCloseDocument:YES];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (SRSettings *)sRSettings {
return [[self document] sRSettings];
}
- (IBAction)backAction:(id)sender
{
[[self sRSettings] decSegmentCount];
[self displaySegmentCountValue];
}
- (IBAction)nextAction:(id)sender
{
[[self sRSettings] incSegmentCount];
[self displaySegmentCountValue];
}
- (void)displaySegmentCountValue
{
[display setIntValue:[[self sRSettings] segmentCount]];
[display2 setIntValue:[[self sRSettings] totalSegments]];
[[self sRSettings] calcProgress];
[progressBar setDoubleValue:[[self sRSettings] progress]];
}
@end
---------------------------------------------------------------
#import <Cocoa/Cocoa.h>
@class SRDocument;
@interface SRSettings : NSObject
{
SRDocument *sRDocument;
@public
NSMutableArray *bookmarks;
NSMutableArray *filesArray;
int segmentCount;
int totalSegments;
double progress;
}
- (id)initWithDocument:(SRDocument *)document;
- (SRDocument *)sRDocument;
@end
-----------------------------------------------------------------------
#import "SRSettings.h"
#import "SRDocument.h"
@implementation SRSettings
- (id)init
{
return [self initWithDocument:nil];
return self;
}
- (id)initWithDocument:(SRDocument *)document {
if (self = [super init]) {
sRDocument = document;
filesArray = [NSArray arrayWithObjects:@"ttlptt001", @"aa001",
@"aa002", @"aa003", @"aa003a", @"aa004", @"aa005", @"aa006", @"aa007",
@"aa008", @"aa009", @"aa010", nil];
totalSegments = [filesArray count];
[filesArray retain];
}
return self;
}
- (void)dealloc
{
[filesArray release];
[super dealloc];
}
- (SRDocument *)sRDocument {
return sRDocument;
}
- (NSArray *)filesArray {
return filesArray;
}
- (int)segmentCount {
return segmentCount;
}
- (int)totalSegments {
return totalSegments;
}
- (int)decSegmentCount
{
segmentCount--;
return segmentCount;
}
- (int)incSegmentCount
{
segmentCount ++;
return segmentCount;
}
- (double)calcProgress
{
double segCnt, ttlSeg;
segCnt = [self segmentCount];
ttlSeg = [self totalSegments];
progress = ((segCnt / ttlSeg) * 100);
return progress;
}
- (double)progress {
return progress;
}
@end
-------------------------------------------------------------------------
#import <Cocoa/Cocoa.h>
@class SRSettings;
@interface SRDocument : NSDocument
{
SRSettings *sRSettings;
}
- (SRSettings *)sRSettings;
@end
--------------------------------------------------------------------------
#import "SRDocument.h"
#import "SRSettings.h"
#import "SRControls.h"
@implementation SRDocument
- (id)init {
if (self = [super init]) {
sRSettings = [[SRSettings allocWithZone:[self zone]]
initWithDocument:self];
}
return self;
}
- (void)dealloc {
[[self sRSettings] release];
[super dealloc];
}
- (SRSettings *)sRSettings {
return sRSettings;
}
- (void)makeWindowControllers {
SRControls *controller = [[SRControls allocWithZone:[self zone]]
init];
[self addWindowController:controller];
[controller release];
}
@end
Can anyone point out (preferably in terms adapted for idiots) what I'm
doing wrong? I feel like a moron asking for help with this, since it's
such a fundamental part of Cocoa, but I'm desperate. Oh - incidental but
crucial bit of information - this is NOT a doc-based app.
- Jeremy.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.