简体   繁体   English

无法更改NSTextField文本

[英]can't change NSTextField text

I have a problem with NSTextField - that I can't change the text in it (can change it once, in (void)awakeFromNib , but not after that). 我对NSTextField有问题-我无法更改其中的文本 (可以在(void)awakeFromNib中更改一次,但此后不能更改)。

I'll give you my header and implementation here: 我将在这里给我标题和实现:

#import <Cocoa/Cocoa.h>
#import "Employee.h"


@interface EmployeeView : NSViewController {  
    NSTextField *mikk;  
    Employee *employee;  
}

@property (retain) IBOutlet NSTextField *mikk;  
@property (retain) Employee *employee;

- (void)setSelectedEmployee:(Employee*)employeeIn;

@end


#import "EmployeeView.h"


@implementation EmployeeView

@synthesize mikk, employee;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
{  
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
    if (self) {  
        // Initialization code here.  
    }  

return self;  
}  

- (void)setSelectedEmployee:(Employee*)employeeIn  
{  
    employee = employeeIn;  
    NSLog(@"given employee = %@", employee.fullName);
    [mikk setStringValue: @"hi"];  
}  

-(void)awakeFromNib  
{  
    NSLog(@"i awoke");  
    [mikk setStringValue:@"hello"];
}  

- (void)dealloc  
{  
    [employee release];  
    [mikk release];  
    [super dealloc];  
}  

@end  

It may look fd up, but the main point is that I call the method 它可能看起来像fd,但主要要点是我调用了方法

- (void)setSelectedEmployee:(Employee*)employeeIn

in my appDelegate and give it the "Employee" that's selected, thus the instance has an employee to work with. 在我的appDelegate中,并为其提供所选的“雇员”,因此该实例具有可与之合作的雇员。 I know for sure, it gets the given employee, because NSLog always prints it out correctly. 我肯定知道,它可以获取给定的雇员,因为NSLog总是正确地打印出来。
The NSTextField called "mikk" is correctly linked in IB, cause it sets the labels text to "hello" in the awakeFromNib method. 名为“ mikk”的NSTextField在IB中已正确链接,因为它在awakeFromNib方法中将标签文本设置为“ hello”。 So the main problem is: why I can't set the labels text elsewhere? 所以主要的问题是: 为什么我不能在其他地方设置标签文本?

I know this looks weird/stupid, but please let me know, if you'd like any more information (also what, if you can be more specific) and suggestions to make this text more readable. 我知道这看起来很奇怪/愚蠢,但是,如果您想了解更多信息(如果可以的话,还可以提供什么信息)以及使该文本更具可读性的建议,请告诉我。

Edit : Looked over the code and left only the "i-think-important-bits" in. Also retained the IBOutlet without any effect on the goal. 编辑 :查看代码,仅保留“ i-think-important-bits”。还保留了IBOutlet,对目标没有任何影响。

Try to change 尝试改变

@property (assign) IBOutlet NSTextField *mikk; 

to

@property (retain) IBOutlet NSTextField *mikk; 

Are you using garbage collection? 您正在使用垃圾收集吗? Here's what the docs on nib loading say about object retention under garbage collection: 以下是笔尖加载文档中有关垃圾回收下的对象保留的内容:

Most objects in the graph are kept in memory through strong references between the objects. 图中的大多数对象通过对象之间的强引用而保留在内存中。 Only the top-level objects in the nib file do not have strong references initially. 最初,只有nib文件中的顶级对象没有强引用。 Thus, your code must create strong references to these objects to prevent the object graph from being released. 因此,您的代码必须创建对这些对象的强引用,以防止对象图被释放。

So, if you don't have a strong reference to the top-level object that contains your text view, probably a view or window, the entire object graph is probably being collected. 因此,如果您对包含文本视图(可能是视图或窗口)的顶级对象没有很好的引用,则可能正在收集整个对象图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM