Error: [DataDisplayViewController - ddvcLoadedRcv] unrecognized selector sent to instance

35 views Asked by At

Good morning,

I would like to ask for clarification re. a specific instance of the error message noted in the title. I recognize that this error has been discussed numerous times before & I have looked thru' about 10 of those threads to see if I can find an answer to my problem but I cannot so pls. bear with me.

My App uses the iPhone to control the actions of a program running on a PC. It is customized to that program rather than using a generalized VNC. It uses a tabBarController with 4 screens. The viewController associated with the first tab initiates the "model" which is the client that communicates with the server on the PC. The second and third VCs have buttons which send commands to the server via the client. All works fine with those.

My problem is with the fourth VC which is intended to display data received from the server via the client. When the client (i.e. model) receives data for display, I am trying to use NSNotification (with an attached array of data in userinfo) to send a message to the fourth VC to tell it to update its labels' text properties. My first attempt resulted in an error message similar to the one in the question title. I hypothesized that this was because the 4th VC had not been created at the time the first data was sent to it. To avoid this, I added a condition in the model so it will not send data until it receives a message from the 4th VC once it is loaded. However, when the execution reaches the line in the 4th VC's ViewDidAppear where the notification is sent, I get the error message in the title.

Again, reading other threads, I concluded that it cannot find the selector in the model class because the model has gone out of memory and been replaced by the 4th VC. Hence it looks for the selector in the 4th VC and cannot find it. So, with apologies for that long pre-amble, my question is how do I keep my "model" in memory so that the notification has an appropriate destination.

Here is some code from the 4th VC showing the notification set up, etc.

//  DataDisplayViewController.m

#import "DataDisplayViewController.h"
#import "VVViewController.h"
#import "VVC_Communication_Module.h"

@interface DataDisplayViewController ()
@property (nonatomic, strong) NSMutableArray *rideData;
@end

@implementation DataDisplayViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ddvcLoadedRcv) name:@"ddvcLoadedEvent" object:nil];
}

- (void) viewDidAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ddvcLoadedEvent" object:nil]; 
}

@end

Here is some code from the model showing the selector that is supposed to get the notification.

//  VVC_Communication_Module.m

#import "VVC_Communication_Module.h" 
#import "VVViewController.h"
#import "DataDisplayViewController.h"

@interface VVC_Communication_Module()
@property (nonatomic, strong) NSArray *itemArray;
@property (nonatomic) BOOL ddvcLoaded;
@end

@implementation VVC_Communication_Module
@synthesize itemArray;
@synthesize ddvcLoaded = _ddvcLoaded;

- (BOOL) ddvcLoaded
{
    _ddvcLoaded = NO;
    return _ddvcLoaded;
}

- (void) ddvcLoadedRcv:(NSNotification *) notification
{
    _ddvcLoaded = YES;
}

@end

Finally, some code from the 1st VC that instantiates the model:

//  VVViewController.m

#import "VVViewController.h"
#import "DataDisplayViewController.h"
#import "VVC_Communication_Module.h"

@interface VVViewController ()
@property (nonatomic, strong) VVC_Communication_Module *moduleRxTx;
@end

@implementation VVViewController
@synthesize moduleRxTx = _moduleRxTx;

- (VVC_Communication_Module *)moduleRxTx
{
    if (!_moduleRxTx) _moduleRxTx = [[VVC_Communication_Module alloc] init];
    return _moduleRxTx;
}

@end

Thank you for all and any help you can offer. RB

2

There are 2 answers

2
thorb65 On

you enter @selector(ddvcLoadedRcv) but it should be @selector(ddvcLoadedRcv:) because your method takes an argument. furhermore you tell your notification that the method ddvcLoadedRcv: is found in "self" but you must point to an object of type VVC_Communication_Module where the method is defined

0
rbwater On

My problem may have been caused by a dumb noobie misunderstanding but in case anyone else has the same problem, I will quickly explain what I did wrong.

It turned out that both the notification set up:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ddvcLoadedRcv) name:@"ddvcLoadedEvent" object:nil];

and the selector:

- (void) ddvcLoadedRcv:(NSNotification *) notification

had to be in the same Class, i.e. VVC_Communication_Module.

The notification post remained in the Class sending the message (DataDisplayViewController).

However, I also had to put the notification set up in an initialize Class method and make the selector a Class method as described in the thread "NSNotification addObserver: someOtherClass"

So finally the relevant part of VVC_Communication_Module.m looked like this:

#import "VVC_Communication_Module.h"
#import "VVViewController.h"
#import "DataDisplayViewController.h"

@interface VVC_Communication_Module()
+ (void) initialize;
@end

@implementation VVC_Communication_Module

+ (void)initialize
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ddvcLoadedRcv:) name:@"ddvcLoadedEvent" object:nil];
}

+ (BOOL) ddvcLoadedRcv:(NSNotification *) notification
{
    return YES;
}

I had to change the selector to return a BOOL which I used rather than the original ddvcLoaded BOOL.

RB