简体   繁体   中英

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).

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

- (void)setSelectedEmployee:(Employee*)employeeIn

in my appDelegate and give it the "Employee" that's selected, thus the instance has an employee to work with. I know for sure, it gets the given employee, because NSLog always prints it out correctly.
The NSTextField called "mikk" is correctly linked in IB, cause it sets the labels text to "hello" in the awakeFromNib method. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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