设置 NSTextField 的值
我正在尝试设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您确定从
anotherFuntion
调用消息时会调用该消息吗?如果anotherFuntion
是另一个类的方法,则调用[self message:]
将无法按您的预期工作...Are you sure message is called when you call it from
anotherFuntion
? IfanotherFuntion
is a method of another class, calling[self message:]
won't work as you expected to...我知道这是一篇旧文章,但我今天一直在摆弄同样的问题。
您必须在文本字段中返回字符串值:
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:
该代码
很不寻常,特别是
new
。我假设 new 只是一个类方法,正常的 alloc/init 相当于主要问题是 IBOutlet仅当使用 Nib 文件加载类
TestMessage
时,NSTextField *text 才会被初始化。它必须在 Interface Builder 中命名为对象的类,如下所示和您必须实现类似这样的
initWithCoder
和encodeWithCoder
才能从 IB 编码中提取字段值:从根本上来说,
IBOutlet
字段不会得到无论您在何处创建该类的实例,都已连接。如果确实如此,您如何表达字段 A 应连接到 UI 对象 A,字段 B 应连接到 UI 对象 B?仅在从 Nib 文件加载类的上下文中建立连接。The code
is unusual, specifically
new
. I'm going to assume thatnew
is just a class method does normalalloc
/init
equivalent toThe main problem is that
IBOutlet NSTextField *text
will be initialized only if the classTestMessage
is loaded with a Nib file. It would have to be named as the class of an object in Interface Builder, like soand you would have to implement
initWithCoder
andencodeWithCoder
something like this in order to extract your field value from the IB encoding: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.