Search code examples
objective-cxcodecocoa-bindings

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


Solution

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