如何从其他实现文件的类方法访问ivar

发布于 2024-08-27 05:28:19 字数 109 浏览 7 评论 0 原文

如何从其他实现文件的类方法中访问实例变量的属性(标题、状态……)?我尝试过 @synthesize 但无法让它工作。更准确地说;我需要访问 NSWindowController 类的 IBOutlets。

How do I access properties (title, state,...) of instance variables from within a class method of an other implementation file? I tried @synthesize but I couldn't get it to work. To be more precise; I need to access IBOutlets of an NSWindowController class.

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

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

发布评论

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

评论(3

鹤仙姿 2024-09-03 05:28:19

首先,您应该先阅读本章。

Objective-C 编程语言简介

你到底想知道什么。显然,没有实例就无法访问实例变量。类方法是无需任何对象实例即可访问的静态方法(消息)。大卫,你能具体说明一下你的问题吗?

First of all, you should read this chapter before.

Introduction to The Objective-C Programming Language.

What do you want to know exactly. Obviously, you cannot access an instance variable without instance. A class method is a static method (message) you can access without any object instance. Could you precise your question David ?

胡渣熟男 2024-09-03 05:28:19

好的,那么您只需在类接口中声明您的属性即可。您的实例变量以 IBOutlet 为前缀,表示必须使用 nib 设置它们。
也许你已经知道所有这些东西了。抱歉,在这种情况下。

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -    
  MyClass.h file
*/
@interface MyClass
{

// instance vars
IBOutlet NSString *title; // Do you have this ? Should be bind in IB.
}

// and this to declare the accessors as public methods
@property (nonatomic, retain) NSString *title;

/*
other methods signature declaration
*/
@end
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -    
  MyClass.m file
*/
@implementation MyClass

@synthesize title; // allow to generate the accessors of your property

/*
methods implementation here
*/

@end

如果实例化您的类,只需调用访问器 [myObjectOfMyClass title]。
也许会看到单例设计模式,它是最常用和最有用的模式之一,可以轻松检索必须唯一的对象实例。
你的 Objective-C 单例是什么样的?

Vincent Zgueb

Ok then you just have to declare your properties in your class interface. Your instance variables are prefixed by IBOutlet to indicate that they have to be set using the nib.
Maybe you already know all that stuff. Sorry in that case.

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -    
  MyClass.h file
*/
@interface MyClass
{

// instance vars
IBOutlet NSString *title; // Do you have this ? Should be bind in IB.
}

// and this to declare the accessors as public methods
@property (nonatomic, retain) NSString *title;

/*
other methods signature declaration
*/
@end
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -    
  MyClass.m file
*/
@implementation MyClass

@synthesize title; // allow to generate the accessors of your property

/*
methods implementation here
*/

@end

If you instantiate your class simply call the accessor [myObjectOfMyClass title].
Maybe see the singleton design pattern which is one of the most used and useful to retrieve easily an object instance that have to be unique.
What does your Objective-C singleton look like?

Vincent Zgueb

孤独患者 2024-09-03 05:28:19

我通常使用我的应用程序控制器作为我需要在所有类中访问的东西的中介......假设你的应用程序控制器也是你的应用程序的委托。从任何类我都可以使用 [NSApp delegate] 访问我的应用程序控制器(应用程序委托)。

考虑到这一点,我确保我的应用程序控制器实例化窗口控制器之类的东西。然后,如果我需要访问窗口控制器,我会在应用程序控制器中为其创建一个实例变量,然后为该实例变量创建一个访问器方法。例如:

在 appcontroller.h 中:

MyWindowController *windowController;
@property (readonly) MyWindowController *windowController;

在 appcontroller.m 中:

@synthesize windowController;

然后,我可以从任何类中使用以下方法访问窗口控制器的该实例:

MyWindowController *windowController = [[NSApp delegate] windowController];

I usually use my appcontroller as an intermediary for things I need accessible throughout all of my classes… assuming your appcontroller is also your application's delegate. From any class I can get to my appcontroller (app delegate) using [NSApp delegate].

With this in mind, I make sure my appcontroller instantiates things like window controllers. Then if I need to access the window controller I create an instance variable for it in my appcontroller and then create an accessor method for that instance variable. For example:

in appcontroller.h:

MyWindowController *windowController;
@property (readonly) MyWindowController *windowController;

in appcontroller.m:

@synthesize windowController;

Then from any class I can get to that instance of the window controller using:

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