在所有类中使用 IBOutlet

发布于 2024-11-30 23:52:39 字数 754 浏览 1 评论 0原文

我已经查阅了与到目前为止我所拥有的以下代码类似的线程。不幸的是,这并没有按计划进行。以下代码不会产生错误/警告,但不会执行任何操作。我想要 IBOutlet 类级别,因为它将在类级别方法中使用,如下所述。

#import <Foundation/Foundation.h>

@interface Interface : NSObject <NSApplicationDelegate> {NSTextView* textView;}

@property (nonatomic, retain) IBOutlet NSTextView* textView;

+ (void)NSPrint:(NSString*)aString;

@end

同时在 Interface.m..

 #import "Interface.h"

@implementation Interface
@synthesize textView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [Interface NSPrint:@"Have fun with this program!"];
}
+ (void)NSPrint:(NSString*)aString
{
    [[[[[Interface new] textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
}

I've consulted a similar thread to the following code that I have so far. Unfortunately, this doesn't work as planned. The following code produces no error/warning but will do nothing. I want the IBOutlet class level because it will be used in a class level method as stated below.

#import <Foundation/Foundation.h>

@interface Interface : NSObject <NSApplicationDelegate> {NSTextView* textView;}

@property (nonatomic, retain) IBOutlet NSTextView* textView;

+ (void)NSPrint:(NSString*)aString;

@end

meanwhile in Interface.m..

 #import "Interface.h"

@implementation Interface
@synthesize textView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [Interface NSPrint:@"Have fun with this program!"];
}
+ (void)NSPrint:(NSString*)aString
{
    [[[[[Interface new] textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
}

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

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

发布评论

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

评论(1

2024-12-07 23:52:40

我不太确定你在这里做什么。 NSPrint 创建一个 Interface 实例,并更改其实例变量之一的属性。然后该方法结束。

您希望从中观察到什么?您没有在 NSPrint: 中创建的 Interface 实例连接到任何东西或执行任何操作 - 您只需创建它并保留它自由漂浮。要实际访问由 [Interface new] 创建的 Interface 实例,请使用类似以下内容:

+(Interface*)NSPrint:(NSString*)aString
{
      Interface* newInterface = [Interface new];
      [[[[newInterface textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
      return newInterface;
}

关于 IBOutlet 的主题:IBOutlet 除了提醒 XCode 的界面构建器它没有任何意义外,它没有任何意义。应该允许您在控制器对象的变量(用 IBOutlet 标记)和界面生成器中创建的另一个对象之间创建连接。然后,当加载视图时,使用连接的对象作为参数来调用控制器对象的变量的 setter 方法。您最终会得到一个控制器类的实例,其实例变量设置为在界面生成器中创建的对象。

有关详细信息,请参阅 Apple 网站上的 Objective-C 手册:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html 或有关界面生成器的信息:http://developer.apple.com/library/mac/#recipes/xcode_help-interface_builder/_index.html#//apple_ref/doc/uid/TP40009971

如果您需要一个类变量(即,连接到类而不是该类的特定实例的变量),你就有问题了。看看这个问题以了解更多信息:Objective C Static Class Level Variables

I'm not really sure what you're doing here. NSPrint creates a instance of Interface, and changes a property of one of its instance variables. Then the method ends.

What are you expecting to observe from this? You don't have the instance of Interface created in NSPrint: connected to anything or doing anything- you just create it and leave it floating free. To actually access the instance of Interface created by [Interface new], use something like:

+(Interface*)NSPrint:(NSString*)aString
{
      Interface* newInterface = [Interface new];
      [[[[newInterface textView] textStorage] mutableString] appendFormat:@"%@\n",aString];
      return newInterface;
}

On the subject of IBOutlet: IBOutlet has no meaning except to alert XCode's interface builder that it should allows you to create connections between a controller object's variable (marked with IBOutlet) and another object created in the interface builder. Then, when the view is loaded, the controller object's variable's setter method is called using the connected object as the argument. You end up with an instance of the controller class with its instance variable set to an object created in the interface Builder.

For more info on that, look in the Objective-C manual on the Apple website: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html or at the information about interface builder: http://developer.apple.com/library/mac/#recipes/xcode_help-interface_builder/_index.html#//apple_ref/doc/uid/TP40009971

If you need a class variable (that is, a variable connected to a class and not to a specific instance of that class), you have a problem. Take a look at this question for more on that: Objective C Static Class Level variables

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