开发者

Variables in separate class coming back null

开发者 https://www.devze.com 2023-03-01 12:08 出处:网络
Ok, I think the question I had here was long-winded and difficult to get through. I\'ll simplify my question:

Ok, I think the question I had here was long-winded and difficult to get through. I'll simplify my question:

  • I have a class called InController.
  • InController has a method called nextPage that tells an int variable, inPageNumber, to add one onto itself and to call on another InController method called updateTable.
  • updateTable clears a table, inTable, of its current data and fills it with data relevant to the page number it retrieves from inPageNumber.
  • The table, inTable, is contained inside an NSBox with specific printing requirements.
  • I subclassed NSBox into a class called CustomViewPagination to meet these printing requirements, overriding its paginations methods. Basically, when a new printing page is required, it attempts to print the same area again, but calls on nextPage to fill the table with the data of the sequential page.

With me so far?

One of the pagination methods I overrided in CustomViewPagination, beginPageInRect, is automatically called for each printed page by default. Because of this, I placed a call to my InController method of nextPage, to change the inTable data for the current printing page.

My problem is when I call nextPage (which is a method in InController) from my CustomViewPagination class. It does nothing and when I debug it I find that all the variables required in the method are nil. However, they are the correct values when I call nextPage from inside InController.


File Extracts:

InController.h:

#import <Cocoa/Cocoa.h>
#import "CustomViewPagination.h"

@interface InController : NSObject {
    IBOutlet NSWindow *inPreview;
    IBOutlet CustomViewPagination *inSheet;
    NSArray *iSelectedIn;
    NSMutableArray *records;
    int inPageNumber;
}
@property (nonatomic, retain) NSArray *iSelectedIn;
@property (nonatomic, retain) NSMutableArray *records;

InController.m:

#import "InController.h"

@implementation InController

@synthesize iSelectedIn, records;

- (IBAction) inNextPage:(id)sender {
    inPageNumber = inPageNumber + 1;
    NSLog(@"inPageNumber called ok");
    [self updateIn];
}


- (IBAction)updateInvoice:(id)sender {
    //wipe all current records 开发者_C百科and refresh empty table
    [records removeAllObjects];
    [inPreviewTable reloadData];
    for (NSArray *s in [[iSelectedIn valueForKey:@"inJobList"] lastObject]) {
        NSString *jLT = [s valueForKey:@"inJT"];
        NSString *jLH = [s valueForKey:@"inJHo"];
        NSString *jLC = [s valueForKey:@"inJC"];
        // etc.
        // if CustomViewPagination called this, records is nil, so nothing
        // is cleared, and there's no *s for iSelectedIn as iSelectedIn
        // is found to be nil. If InController called this, it works fine.

CustomViewPagination.h:

#import <Cocoa/Cocoa.h>

@class InController;

@interface CustomViewPagination : NSBox {
    InController *inControllerInstance;
}

@end

CustomViewPagination.m:

#import "CustomViewPagination.h"
#import "InController.h"

@implementation CustomViewPagination

- (void) awakeFromNib {
    inControllerInstance = [[InController alloc] init];
}

- (void)beginPageInRect:(NSRect)aRect atPlacement:(NSPoint)location {
    int pageCounter = [[NSPrintOperation currentOperation] currentPage];
    if (pageCounter == 1) {
        // Don't respond to 1st page, do nothing.
    } else {
        [inControllerInstance inNextPage:self];
    }
    [super beginPageInRect:aRect atPlacement:location];
}

@end


You are using 2 IBOutlets in InController (inPreview & inSheet), but InController is created programmatically in CustomViewPagination's awakeFromNib.

How are the Outlets connected? (Can't be from within IB, as you are creating the InController instance programmatically). This would be an explanation why both are nil.

0

精彩评论

暂无评论...
验证码 换一张
取 消