在Objective-C 中,如何让@property 可以被其他类访问?

他们的

原始问题仍然在此更新下方:

所以进一步的研究表明我的

“...缺少 setter 或实例变量”

日志消息是由于.xib.

我最初认为可能是这种情况,这就是为什么我在图形界面构建器中经历了重新连接插座和属性的过程,但这似乎不足以修复连接。

我将插座恢复为属性而不是 iVar 并再次重新连接,但仍然无济于事。所以我正在从头开始重新制作 .xib。请继续关注结果。

原问题如下:

在父类和工作表类中声明和合成属性,并尝试通过它们各自的 class.property 名称访问属性,Xcode 拒绝代码。

我最近发布了一个类似的问题,并在被告知没有足够的信息做出回应后删除了它,所以我在下面添加了一个迷你应用程序,它显示了相关设置在超过 2000 行 Objective-C 的真实应用程序中的情况,在我尝试添加父/工作表属性功能之前,它已正确构建和运行。

我已经用前缀////. 当我注释掉错误的行时,应用程序及其.xib的构建和运行当然功能失调。

父类.h

// ParentClass stuff belongs in the original main window controller
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@interface ParentClass : NSObject
{
    IBOutlet NSTextField * messageTextField;
    IBOutlet NSButton    * proceedButton;
}
@property (assign)  IBOutlet  NSWindow * window;
@property (strong)  NSMutableString * parentPropInfo;
- (IBAction) awakeFromNib;
- (IBAction) doCreate:(id)sender;
@end

父类.m

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "ParentDelegate.h"
#import "ParentClass.h"
#import "SheetClass.h"
@implementation ParentClass
ParentDelegate     * MyDelegate;  // only confirms termination requests
NSWindowController * SheetController;
@synthesize parentPropInfo;
- (IBAction)awakeFromNib  {
    MyDelegate = [NSApplication sharedApplication].delegate;
    MyDelegate.ParentController = self;   // BTW, this property assignment works!
    SheetController = [[SheetClass alloc] initWithWindowNibName: @"SheetClass"];
    messageTextField.stringValue = @"Click Proceed button";
}
- (IBAction)doProceed*emphasized text*:(id)sender  {
    parentPropInfo = @"Hello!".mutableCopy;   // to be read by the sheet
    [NSApp runModalForWindow:SheetController.window];
    // Sheet is active now until it issues stopModal, then:

    messageTextField.stringValue = SheetController.sheetPropInfo;   // set by the sheet
////above gets ERROR "Property sheetPropInfo not found on object of type 'NSWindowController *'"

    messageTextField.stringValue = SheetController.window.sheetPropInfo;
////above gets ERROR "Property sheetPropInfo not found on object of type 'NSWindow *'"

    [NSApp endSheet: SheetController.window];
    [SheetController.window orderOut:self];
}
@end

SheetClass.h

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "ParentClass.h"
@interface SheetClass : NSWindowController
{
    IBOutlet NSTextField * propTextField;
    IBOutlet NSButton    * cancelButton;
}
@property (assign) IBOutlet NSWindow * window;
@property   NSMutableString * sheetPropInfo;
- (IBAction)awakeFromNib;
- (IBAction)doCancel:(id)sender;
@end

SheetClass.m

#import "SheetClass.h"
#import "ParentClass.h"
@implementation SheetClass
@synthesize sheetPropInfo;
- (IBAction)awakeFromNib  {

    propTextField.stringValue = self.window.sheetParent.parentPropInfo;  // set by the parent
////above gets ERROR "Property parentPropInfo not found on object of type 'NSWindow *'"

    sheetPropInfo = @"Goodbye!".mutableCopy;  // to be read by the parent
}
- (IBAction)doCancel:(id)sender  {
    [NSApp stopModal];
}
@end

我在 Apple 文档或广泛的(现在三个星期!)在线搜索中找不到任何内容来提供有关我无知的深刻见解。对于说明我的问题所需的大量代码,我深表歉意!我从哪里获得我需要的信息?

哑光

错误信息非常清楚。只需阅读它们并思考它们。我们只看第一个。你是说:

messageTextField.stringValue = SheetController.sheetPropInfo;

...并从编译器获得此响应:

// Property sheetPropInfo not found on object of type 'NSWindowController *'

好吧,考虑一下表达式SheetController.sheetPropInfo以及为什么编译器无法理解它。您已按如下方式声明 SheetController:

NSWindowController * SheetController;

这就是编译器所知道的全部内容:SheetController 是一个 NSWindowController。嗯,果然,正如编译器所说,sheetPropInfo 不是 NSWindowController 的属性。它是 SheetClass 的一个属性(与 NSWindowController 不同;它是 NSWindowController 的子类)。

如果你知道 SheetController 实际上是一个 SheetClass 实例,你需要告诉编译器这个事实。您必须将 SheetController 声明为 SheetClass 或将其从 NSWindowController 转换为 SheetClass。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章