可可文本字段返回 null
抱歉,如果有点长,但我想尽可能具有描述性。 我正在制作一个包含多个类和 2 个 nib 文件的测试应用程序(每个类都是一个 nib 的所有者)。其中一个 nib 文件有一个文本字段,另一个有一个按钮。当按下另一个笔尖视图中的按钮时,我试图记录文本字段内的所有内容,但它返回(空)。在其中一个类(ViewClass)中,我有这样的:
- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSString *string= [anInstance name];
NSLog(@"startAction logged: %@", string);
“Name”是 MyClass 的一个属性。我想要做的是在 MyClass 的 init 中设置“name”,这样,当初始化 anInstance 时,MyClass init 方法会执行以下操作:
- (id)init {
if ( self = [super init] ) {
[self setName:[nameInput stringValue]];
NSLog(@"init value: %@", name);
}
return self;
“NameInput”是文本字段。我认为这会返回文本字段中的任何内容,但我得到的是 null 。当我使用 setName:@"text"
时,它可以正常传递,因此文本字段出现问题。
我之前用我自己的 getter 做到了这一点,在这种情况下,当从它自己的类调用该方法时,它不会返回 null,但如果它是从另一个类初始化并调用的,那么它会返回 null,我使用了这个:
- (NSString *)name {
NSLog(@"nameMethod = %@", [nameInput stringValue]);
return [[[nameInput stringValue] retain] autorelease];
这样,我可以告诉它一切都已正确设置,但是当我从 ViewClass 初始化 MyClass 并尝试获取“名称”时,会发生一些情况,它一直说文本字段为空。
不确定它是否有帮助,但带有按钮的笔尖属于 MyView (它是 NSViewController 的子类),文本字段属于 MyClass (NSObject 的子类)。
有人建议该字段没有正确链接,但如果这是真的,那么当从它自己的类调用时它就不会起作用,但它确实起作用了。其他人提到这可能是文本字段初始化为 nil 的问题,所以我尝试了上面的 init 方法。到目前为止,两者都没有发挥作用。
感谢您的帮助。
Sorry if it's a bit long but I want to be as descriptive as possible.
I'm making a test application with several classes and 2 nib files (each class is the owner of one nib). One of the nib files has a textfield, the other has a button. I'm trying to log whatever is inside the text field when the button in the other nib view is pressed, but it returns (null). In one of the classes (ViewClass) I have this:
- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSString *string= [anInstance name];
NSLog(@"startAction logged: %@", string);
"Name" is a property of MyClass. What I want to do is have "name" set in the init of MyClass, that way, when anInstance is initialized, the MyClass init method does this:
- (id)init {
if ( self = [super init] ) {
[self setName:[nameInput stringValue]];
NSLog(@"init value: %@", name);
}
return self;
"NameInput" is the textfield. I thought this would return whatever was in the textfield, but I get null instead. When I use setName:@"text"
it gets passed fine, so something is wrong with the text field.
I previously did this with my own getter, and in that case, it didn't return null when the method was called from it's own class, but if it was initialized and called from the other class, then it returned null, I used this:
- (NSString *)name {
NSLog(@"nameMethod = %@", [nameInput stringValue]);
return [[[nameInput stringValue] retain] autorelease];
This way, I can tell it is all properly set up, but something is happening when I init MyClass from the ViewClass, and try to get "name", that it keeps saying that the text field is null.
Not sure if it helps but the nib with the button belongs to MyView (which is a subclass of NSViewController) and the textfield belongs to MyClass (subclass of NSObject).
Someone suggested the field was not properly linked, but if that was true, it wouldn't have worked when called from it's own class, but it did. Someone else mentioned it might be a problem with the textfield being initialized to nil, so I tried the init thing above. Neither has worked so far.
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
awakeFromNib
之前(即对象的 init 方法运行之后),IBOutlet 不能保证被连接。您需要在awakeFromNib
中进行笔尖设置。我回家后编辑:抱歉,我之前没有仔细阅读。我上面说的是事实,但还有更深层次的问题。我现在发现您也在处理两个不同的对象 - 一个在笔尖中,一个在代码中创建。如果笔尖中有一个对象,其出口连接到界面元素,则不会使该类的其他对象也有引用该元素的实例变量。两个独立创建的 MyClass 实例不会共享相同的实例变量,就像程序中的每个 NSArray 都不会保存相同的项目集一样。如果您想使用笔尖中的实例,则需要使用那个实例。
如何做到这一点取决于您如何构建程序。没有简单的
[self magiclyGetObjectFromNib]
。不知何故,一个对象需要要么找到另一个对象(例如,通过知道笔尖的所有者),要么通过一个知道这两个对象的对象来告知另一个对象。IBOutlets are not guaranteed to be hooked up until
awakeFromNib
, which is after your objects' init methods have run. You'll need to do your nib setup inawakeFromNib
.EDIT NOW THAT I'VE GOTTEN HOME: Sorry, I didn't read carefully enough before. What I said above was true, but there's a deeper problem as well. I see now that you also are dealing with two different objects — one in the nib, one created in code. If you have an object in a nib with an outlet hooked up to an interface element, that doesn't make other objects of that class also have an instance variable referring to the element. Two independently created MyClass instances don't share the same instance variables any more than every NSArray in your program holds the same set of items. If you want to use the instance from the nib, you'll need to use that instance.
How you do this is a matter of how you structure your program. There's no simple
[self magicallyGetObjectFromNib]
. Somehow, one object needs to either find the other (say, by knowing the nib's owner) or be told about the other by an object that knows about them both.当您尝试从同一对象的字段中提取值时,您正处于 init 例程中,并且显然希望它被初始化。 '这不会发生。
You're in the init routine when you try to extract a value from a field in the same object, and apparently expect it to be initialized. 'Tain't gonna happen.