单独类中的变量返回 null

发布于 2024-11-04 07:45:29 字数 3325 浏览 0 评论 0原文

好吧,我觉得我这里提出的问题很啰嗦,很难解答。我将简化我的问题:

  • 我有一个名为 InController 的类。
  • InController 有一个名为 nextPage 的方法,它告诉 int 变量 inPageNumber 将一个变量添加到自身上并调用另一个 InController< /strong> 调用 updateTable 的方法。
  • updateTable 清除表inTable 的当前数据,并用与从inPageNumber 检索的页码相关的数据填充它。
  • 该表 inTable 包含在具有特定打印要求的 NSBox 内。
  • 我将 NSBox 子类化为一个名为 CustomViewPagination 的类来满足这些打印要求,并重写其分页方法。基本上,当需要新的打印页面时,它会尝试再次打印同一区域,但调用 nextPage 用连续页面的数据填充表格。

到目前为止和我在一起吗?

默认情况下,会自动为每个打印页面调用我在 CustomViewPagination 中重写的分页方法 beginPageInRect。因此,我调用了 nextPageInController 方法,以更改当前打印页面的 inTable 数据。

我的问题是当我从我的 CustomViewPagination 类调用 nextPage (这是 InController 中的一个方法)时。它什么都不做,当我调试它时,我发现该方法中所需的所有变量都为零。但是,当我从 InController 内部调用 nextPage 时,它们是正确的值。


文件提取:

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 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

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-11-11 07:45:29

您在 InController 中使用 2 个 IBOutlet(inPreviewinSheet),但 InController 是在 中以编程方式创建的>CustomViewPagination 的 awakeFromNib

奥特莱斯如何连接? (不能来自 IB 内部,因为您正在以编程方式创建 InController 实例)。这可以解释为什么两者都是nil

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文