NSNotification selector method not working well

I've set up a notification added to a NSButton:

Class X

post notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"cbCheckBoxEntred" object:nil];


Class Y

setup observer:

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(doMyStuff:)

name:@"cbCheckBoxEntred" object:nil];


calling the method:

- (void)doMyStuff:(NSNotification *)note {

NSString *myImagePathStr = [tfImagePath stringValue];

justAnotherObjCclass *icmjustAnotherObjCclass = [[justAnotherObjCclass alloc] init];

NSImage* myImage = [icmjustAnotherObjCclass convertImage:myImagePathStr];

}

The method: doMyStuff is called as expected

BUT the function:

NSString *myImagePathStr = [tfImagePath stringValue];

returns allways nil


similar functions in the method "convertImage" called from another class:

NSImage* myImage = [icmjustAnotherObjCclass convertImage:myImagePathStr];

are also returning nil


What am I doing wrong ?

Can someone assist me ?

Answered by QuinceyMorris in 248826022

>> If you have an idea of the cause, it should be very helpfull anyway.


Only a guess, that somehow you have two instances of the Y view, one that the user can see where the string was typed, and the other hidden (or behind it, or the wrong size, or the wrong position, or whatever) where its text field remains empty.


There are a couple of ways this can happen by accident, and it's not easy to tell that there are 2 views instead of only one.

What kind (class) of object is tfImagePath?


If it's a control, it's value is generally referred to as "objectValue". "stringValue" is for converting values (e.g. if the objectValue is a NSNumber), but it doesn't produce a valid result for all classes of receiver.


If tfImagePath is, for example, a NSPathControl, try getting its value from its "url" property instead.

tfImagePath is a NSTextField, where I like to get the string value.


But I have the same problem with all kinds of objects from a Window, such as NSButton, NSComboBox etc.

If I access the same method from elsewhere in the application there is no problem to get these values, If the method is called from the NSnotification I can't get those values.

Then you need to do a bit of investigating. Is "tfImagePath" nil? Or is it non-nil but returning a nil value? What is its "objectValue"? What kind of class is Y? Is it a window controller? Are you sure you don't have 2 different objects of class Y?

Hi,

Class Y is a subclass of NSView.

"tfImagePath" NSTextField is recognized, but not his content.


I moved the NSNotification code from class Y to the application delegate, and now everything is working fine.

So, no idea what caused the problem, but with this work around it works right now.

If you have an idea of the cause, it should be very helpfull anyway.


Thank you for the effort !

Accepted Answer

>> If you have an idea of the cause, it should be very helpfull anyway.


Only a guess, that somehow you have two instances of the Y view, one that the user can see where the string was typed, and the other hidden (or behind it, or the wrong size, or the wrong position, or whatever) where its text field remains empty.


There are a couple of ways this can happen by accident, and it's not easy to tell that there are 2 views instead of only one.

Thank you very, very much for your 'guess'.



Indeed there were 2 views, by accident, and since quite a long time, without issues till now..

I cleaned up this issue, solved the problem.



Once again, thank you !

NSNotification selector method not working well
 
 
Q