Objective c 单独类中的变量返回空值
好的,我想我这里的问题很冗长,很难理解。我将简化我的问题:Objective c 单独类中的变量返回空值,objective-c,macos,class,variables,Objective C,Macos,Class,Variables,好的,我想我这里的问题很冗长,很难理解。我将简化我的问题: 我有一个名为InControl智能驭享的课程 InControl智能驭享有一个名为nextPage的方法,它告诉一个int变量inPageNumber,将一个添加到自身,并调用另一个名为updateTable的InControl智能驭享方法 updateTable清除表inTable中的当前数据,并用从inPageNumber检索的页码相关的数据填充该表 表格表格包含在带有特定打印要求的NSBox中 我将NSBox子类化为一个名为Cu
- 我有一个名为InControl智能驭享的课程
- InControl智能驭享有一个名为
的方法,它告诉一个int变量nextPage
,将一个添加到自身,并调用另一个名为inPageNumber
的InControl智能驭享方法updateTable
清除表inTable中的当前数据,并用从updateTable
检索的页码相关的数据填充该表inPageNumber
- 表格表格包含在带有特定打印要求的
NSBox中
- 我将
子类化为一个名为CustomViewPagination的类,以满足这些打印要求,覆盖其分页方法。基本上,当需要一个新的打印页面时,它会尝试再次打印相同的区域,但会调用NSBox
,用连续页面的数据填充表格nextPage
beginPageInRect
,会自动为每个打印页面调用。因此,我调用了nextPage
的InControl智能驭享方法,以更改当前打印页面的表格数据
我的问题是当我从我的CustomViewPagination类调用nextPage
(这是InControl智能驭享中的一种方法)时。它什么也不做,当我调试它时,我发现该方法中需要的所有变量都是零。但是,当我从InControl智能驭享内部调用nextPage
时,它们是正确的值
文件摘录: InControl.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;
#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.
#import <Cocoa/Cocoa.h>
@class InController;
@interface CustomViewPagination : NSBox {
InController *inControllerInstance;
}
@end
#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
CustomViewPagination.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;
#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.
#import <Cocoa/Cocoa.h>
@class InController;
@interface CustomViewPagination : NSBox {
InController *inControllerInstance;
}
@end
#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
您在
InControl智能驭享
(inPreview
&inSheet
)中使用了2个IBoutlet,但InControl智能驭享
是在CustomViewPagination的
awakeFromNib
中以编程方式创建的
插座是如何连接的?(不能来自IB,因为您正在以编程方式创建
InControl智能驭享
实例)。这可以解释为什么两者都是nil
您的burbController init方法在哪里?(注意,类名总是以大写字母-BurbController开头。而且在-beginPageInRect方法的末尾有一个错误的分号)好的,这个调查途径与我迄今为止读到的任何东西都不同。我很好奇。我知道你说的基本意思,但我有点不知所措。我已将CustomViewPaginationinSheet
连接到界面中使用重写子类方法的单个CVP对象。原因是我后来在描述在NSPrintOperation
中打印的内容时,会按名称识别它。我如何避免使用插座来执行此操作?或者避免在awakeFromNib中以编程方式创建它,而只使用插座(基本上,使用一个或另一个)。inSheet是InControl智能驭享的插座。要将插座与对象连接,您需要在IB中实例化该对象。如果您这样做,并且您正在CustomViewPagination的awakeFromNib:中创建另一个InControl智能驭享实例,那么您将得到两个不同的InControl智能驭享实例。一个连接在nib内,另一个接收您从beginPageInRect发送的消息:。对我来说,听起来你只是想拥有一个InControl智能驭享。对吗?对:)我一开始就知道你在说什么。我意识到现在我正在使用两个不应该跨越的习惯。我在那里采取了一些主动行动,在CustomViewPagination的awakeFromNib
中删除了InControl智能驭享的init,而是在我的Nib调色板中将一个插座连接到InControl智能驭享。现在我不确定如何解决同一个InControl实例,就像您建议我要做的那样(我也这么做)。您将InControl作为IBOutlet,对吗?因此,您可以将inNextPage发送到InControl智能驭享。只需在beginPageInRect中设置一个断点,并检查InControl智能驭享是否为零。我对你的设计了解不够。但是,您可能会考虑重构一些部件(可能使用NSView控制器),以避免将控制器实例传递到视图类。我可以在nib调色板中很好地导出到该类,但显然我会收到警告,说该类可能不会响应我的方法调用,因为它们不是类级别的。使用IBOutlet传递InControl智能驭享实例最简单的方法是什么。通常在这个阶段,我alloc和init类,但这是创建一个单独的实例,并导致我的悲痛。