在 Objective-C 中,如何让其他 类 可以访问 @属性?
In Objective-C, how to make @property's accessible to other classes?
本次更新后原问题依旧:
所以进一步的研究表明我的
"...missing setter or instance variable"
日志消息是由于精神错乱 .xib
。
我原本以为可能是这种情况,这就是为什么我在图形界面构建器中重新连接插座和属性的过程,但这似乎不足以修复连接。
我将网点恢复为属性而不是 iVars 并再次重新连接,但仍然无济于事。所以我正在从头开始重新制作 .xib。敬请期待结果。
原题如下:
已在父级和 sheet 类 中声明和合成属性,并尝试在其中通过各自的 class.property 名称访问属性,Xcode 拒绝代码。
我最近发布了一个类似的问题,并在被告知没有足够的信息做出回应后将其删除,所以我在下面添加了一个迷你应用程序,它显示了相关设置在 2000 多个真实应用程序中的情况Objective-C 行,在我尝试添加 Parent / Sheet 属性功能之前正确构建和 运行。
我已经用 ////
前缀指示了编译器错误消息。当我注释掉错误行时,应用程序及其 .xib
的构建和运行当然是功能失调的。
ParentClass.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
ParentClass.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 文档或广泛的(现在已经三周了!)在线搜索中找不到任何关于我糟糕的无知运行ce 的见解。对于说明我的问题所需的大量代码,我深表歉意!我应该从哪里获得我需要的信息?
错误信息非常清楚。只是阅读它们并思考它们。让我们只看第一个。你是说:
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。
本次更新后原问题依旧:
所以进一步的研究表明我的
"...missing setter or instance variable"
日志消息是由于精神错乱 .xib
。
我原本以为可能是这种情况,这就是为什么我在图形界面构建器中重新连接插座和属性的过程,但这似乎不足以修复连接。
我将网点恢复为属性而不是 iVars 并再次重新连接,但仍然无济于事。所以我正在从头开始重新制作 .xib。敬请期待结果。
原题如下:
已在父级和 sheet 类 中声明和合成属性,并尝试在其中通过各自的 class.property 名称访问属性,Xcode 拒绝代码。
我最近发布了一个类似的问题,并在被告知没有足够的信息做出回应后将其删除,所以我在下面添加了一个迷你应用程序,它显示了相关设置在 2000 多个真实应用程序中的情况Objective-C 行,在我尝试添加 Parent / Sheet 属性功能之前正确构建和 运行。
我已经用 ////
前缀指示了编译器错误消息。当我注释掉错误行时,应用程序及其 .xib
的构建和运行当然是功能失调的。
ParentClass.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
ParentClass.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 文档或广泛的(现在已经三周了!)在线搜索中找不到任何关于我糟糕的无知运行ce 的见解。对于说明我的问题所需的大量代码,我深表歉意!我应该从哪里获得我需要的信息?
错误信息非常清楚。只是阅读它们并思考它们。让我们只看第一个。你是说:
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。