设置 NSTextField 的值

发布于 2024-11-07 21:55:48 字数 982 浏览 0 评论 0原文

我正在尝试设置 NSTextField 的值,但它无法正常工作。

我有一个链接到 IBAction 的按钮,当我使用 self 设置它时,它工作正常:

#import <Foundation/Foundation.h>


@interface TestMessage : NSObject {
     IBOutlet NSTextField *text;
}

- (IBAction) setMessage: (id) controller;
- (void) Message:(NSString *) myMessage;    
@end

#import "TestMessage.h"

@implementation TestMessage

- (IBAction) setMessage: (id) controller {
    // This works
    [self Message:@"Hello"];

    // but this doesn't
    TestMessage * messageTest= [TestMessage new];
    [messageTest Message:@"Hi"];

}
- (void) Message: (NSString *) myMessage {
    [text setStringValue: myMessage]; 
    NSLog(@"Message Was Called");
    // This returns <NSTextField: 0x1001355b0> when called 
    // using self, but null when called the other way.
    NSLog(@"%@", text);
}

@end

我搜索了一段时间,但仍然找不到回答。

我猜这与代表有关,但我不确定。

提前致谢。

I am trying to set the value of an NSTextField, but it's not working properly.

I have a button linked to an IBAction, and when I set it using self, it works fine:

#import <Foundation/Foundation.h>


@interface TestMessage : NSObject {
     IBOutlet NSTextField *text;
}

- (IBAction) setMessage: (id) controller;
- (void) Message:(NSString *) myMessage;    
@end

#import "TestMessage.h"

@implementation TestMessage

- (IBAction) setMessage: (id) controller {
    // This works
    [self Message:@"Hello"];

    // but this doesn't
    TestMessage * messageTest= [TestMessage new];
    [messageTest Message:@"Hi"];

}
- (void) Message: (NSString *) myMessage {
    [text setStringValue: myMessage]; 
    NSLog(@"Message Was Called");
    // This returns <NSTextField: 0x1001355b0> when called 
    // using self, but null when called the other way.
    NSLog(@"%@", text);
}

@end

I've searched for a while, but still can't find the answer.

I guess it has something to do with the delegate, but I'm not sure.

Thanks in advance.

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

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

发布评论

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

评论(3

[浮城] 2024-11-14 21:55:48

您确定从 anotherFuntion 调用消息时会调用该消息吗?如果 anotherFuntion 是另一个类的方法,则调用 [self message:] 将无法按您的预期工作...

Are you sure message is called when you call it from anotherFuntion? If anotherFuntion is a method of another class, calling [self message:] won't work as you expected to...

猫烠⑼条掵仅有一顆心 2024-11-14 21:55:48

我知道这是一篇旧文章,但我今天一直在摆弄同样的问题。
您必须在文本字段中返回字符串值:

[textField stringValue];

I know this is an old post, but I have been fiddling with the same issue today.
You have to return string value in textfield:

[textField stringValue];
萌吟 2024-11-14 21:55:48

该代码

TestMessage * messageTest = [TestMessage new];

很不寻常,特别是new。我假设 new 只是一个类方法,正常的 alloc/init 相当于

 TestMessage * messageTest = [[TestMessage alloc] init];

主要问题是 IBOutlet仅当使用 Nib 文件加载类 TestMessage 时,NSTextField *text 才会被初始化。它必须在 Interface Builder 中命名为对象的类,如下所示

Set to load this class in IB

和您必须实现类似这样的 initWithCoderencodeWithCoder 才能从 IB 编码中提取字段值:

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.text = [coder decodeObjectForKey:@"text"];
    }
    return self;
}


-(void)encodeWithCoder:(NSCoder *)coder
{
    [super encodeWithCoder:coder];
    [coder encodeObject:self.text forKey:@"text"];
}

从根本上来说,IBOutlet 字段不会得到无论您在何处创建该类的实例,都已连接。如果确实如此,您如何表达字段 A 应连接到 UI 对象 A,字段 B 应连接到 UI 对象 B?仅在从 Nib 文件加载类的上下文中建立连接。

The code

TestMessage * messageTest = [TestMessage new];

is unusual, specifically new. I'm going to assume that new is just a class method does normal alloc/init equivalent to

 TestMessage * messageTest = [[TestMessage alloc] init];

The main problem is that IBOutlet NSTextField *text will be initialized only if the class TestMessage is loaded with a Nib file. It would have to be named as the class of an object in Interface Builder, like so

Set to load this class in IB

and you would have to implement initWithCoder and encodeWithCoder something like this in order to extract your field value from the IB encoding:

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.text = [coder decodeObjectForKey:@"text"];
    }
    return self;
}


-(void)encodeWithCoder:(NSCoder *)coder
{
    [super encodeWithCoder:coder];
    [coder encodeObject:self.text forKey:@"text"];
}

Fundamentally, IBOutlet fields do not get wired up wherever you create an instance of that class. If they did, how would you express that field A should be wired to UI object A and field B should be wired to UI object B? The connection is established only in the context of loading a class from a Nib file.

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