neutrino
neutrino

Reputation: 441

Why is my NSTextField not responding to setStringValue?

I have an object which is hooked up to interface builder (one of the little blue boxes down the left hand side (Xcode 4.5.2) and I have created bindings to an NSTextField as I am used to doing. I have also synthesised the text field in the main file (don't quite understand why but pretty sure this is necessary). However, when I try sending setStringValue:@"a string" to the text field, it doesn't work. Also, when I try and print the text field object to the command line it says null. From googling, I think some people have this problem when they use init instead of awakefromnib but this method in my programme is triggered when I press a button. The code is below. If any more information is needed, let me know. Thanks.

#import <Foundation/Foundation.h>
#import "CSSRuleSet.h"

@interface RuleSetViewUpdater : NSObject
@property (weak) IBOutlet NSTextField *top;

-(void)updateFields:(CSSRuleSet *)RuleSet;


@end

.

#import "RuleSetViewUpdater.h"

@implementation RuleSetViewUpdater

@synthesize top = _top;

-(void)updateFields:(CSSRuleSet *)RuleSet
{
    [_top setStringValue:[RuleSet getValue:@"top"]];
    NSLog(@"%@", _top);
}

@end

xib structure

enter image description here

Upvotes: 0

Views: 1925

Answers (2)

neutrino
neutrino

Reputation: 441

Ok, so I realised what I was doing wrong. Basically, I had connected up the interface builder to the object exactly fine and it would of worked but because of the nature of my programme, I was initialising a new object to control the interface not realising that by doing that it would not have access to the interface. So basically instead I had to pass the original object. That probably does not make any sense but let me know if it needs explaining better.

Upvotes: 2

Marcel
Marcel

Reputation: 6579

First of all you need a connection from your sourcefile to the object in the XIB file. You have an outlet defined, but it's not sure that it's actually connected. Right-click on the object in the XIB file and make sure the outlet is connected.

Your NSLog statement ( NSLog(@"%@", _top); ) logs the NSTextField object itself. The fact that it logs "(null)", means that the outlet is not (yet) connected. When your XIB file gets loaded, the outlets are not immediately connected. awakeFromNib() gets called on all the objects in the XIB file when the file is loaded. After this, you should be able to access the objects by using the outlets.

Upvotes: 0

Related Questions