0

I'm having trouble calling an external method from a NSTextField Subclass. I have a NSTextField subclass that I'm monitoring text with and when textDidChange I'm calling an external method that is of a NSObject class. For some reason my method doesn't get called. I'm hoping someone could explain why. My code is below. Thanks.

EDIT: Fixed code to what drewag suggested.

MyTextField.h

#import <Cocoa/Cocoa.h>
@class ObjectController;

@interface MyTextField : NSTextField <NSTextFieldDelegate> {
    ObjectController *objectController;
}
@property (strong, nonatomic) ObjectController *objectController;

@end

MyTextField.m

#import "MyTextField.h"
#import "ObjectController.h"

@implementation MyTextField
@synthesize objectController;

- (void)textDidChange:(NSNotification *)notification{
[objectController methodFromOtherClass];
}

@end

ObjectController.h

#import <Foundation/Foundation.h>
@interface ObjectController : NSObject {

}

- (void)methodFromOtherClass;
@end

ObjectController.m

#import "ObjectController.h"
@implementation ObjectController

- (void) methodFromOtherClass {
NSLog(@"Testing");
}
0SX
  • 1,252
  • 6
  • 24
  • 47
  • You should forward-declare `ObjectController` in your header (don't `#import` it!) -- `#import` it in your implementation. – Jacob Relkin Jun 24 '13 at 22:13
  • Well, I tried what you said and it still doesn't get called. Any other suggestions? – 0SX Jun 24 '13 at 22:24

3 Answers3

1

There are two likely scenarios causing this issue:

  1. objectController is nil when textDidChange: is called
  2. textDidChange: is not actually being called because you didn't connect it up correctly.

I don't think anyone can help you beyond that since you did not post your connection code nor where you are setting objectController.

drewag
  • 93,393
  • 28
  • 139
  • 128
  • textDidChange get's called. Tested with NSLOG. I'll look into #1 Thanks. – 0SX Jun 24 '13 at 22:38
  • Yup, your right about #1. objectController is null when called. What would cause that? – 0SX Jun 24 '13 at 22:51
  • It isn't so much what is causing it to be nil (in objective-c it is nil not NULL, NULL is something else). It is that you aren't doing anything to make it not nil. It is really tough for me to advise how it should work without knowing your higher level goals. I need to know what the ObjectController is supposed to be controlling and what view / view controller this text field is on. – drewag Jun 25 '13 at 05:44
0

Probably forgot to assign the delegate to self or whatever object that implements the textDidChange? Happens to me a lot.

Ben Wong
  • 385
  • 1
  • 4
  • 5
0

I fixed the issue by creating an instance of ObjectController instead of the above process.

ObjectController *objectController = [ObjectController new];
[objectController methodFromOtherClass];
0SX
  • 1,252
  • 6
  • 24
  • 47
  • Well, Sort of got it working. The method calls the NSLog but it doesn't do other commands in the method. I'm confused. – 0SX Jun 25 '13 at 05:30