I have an iPad app (XCode 4.6, Storyboards, iOS 6.2) with several classes. In one class, when I finish posting, I post a notification which I expect another class to catch. Not happening!
Here is the code to register the notifications, which I have in AppDelegate.m:
// add observers for notificaations
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(calendarTapNotification:)
name:@"calendarDateSelected" object:nil ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(notificationOfAppointmentMade:)
name:@"appointmentMade" object:nil ];
Here is the code where I post a notification for one of the observers:
if(aDate != nil) { // if user tapped outside of valid date range, aDate will be nil
[passedData setObject:aDate forKey:@"selectedDate"];
// post notification CFGregorianDate has been tapped and pass selected date
[[NSNotificationCenter defaultCenter] postNotificationName:@"calendarDateSelected" object:self userInfo:passedData];
}
And this is the code where I handle the "calendarTapNotification" notification which never gets called:
- (void) calendarTapNotification:(NSNotification *) notification {
// was the calendar tapped?
if ([[notification name] isEqualToString:@"calendarDateSelected"]) {
// get the data in the dictiionary
NSDictionary *dict = [notification userInfo];
// NSLog(@"\ndict contents: %@", [dict objectForKey:@"selectedDate"]); // this is NSDate class!
// gets the year, month, and day for selected date
NSDate *selectedDate = [dict objectForKey:@"selectedDate"];
// NSLog(@"\n\ndict-selectedDate: %@", [dict objectForKey:@"selectedDate"]);
NSCalendar *calendar = [NSCalendar currentCalendar]; // get default calendar
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate: selectedDate];
// NSLog(@"\n\ndateFromComponents: %@", components);
// create start and end of date
NSDate *startDate = [self beginningOfDay: [calendar dateFromComponents:components]];
NSDate *endDate = [self endOfDay: [calendar dateFromComponents:components]];
// NSLog(@"\nstartDate: %@ endDate: %@", startDate, endDate);
// create the predicate
NSPredicate *predicate = ([NSPredicate predicateWithFormat:@"((aStartTime > %@) AND (aStartTime <= %@))", startDate, endDate]);
// find all appointments with that selected date (AppointmentInfo is NSManagedContext; appointmentInfo is NSArray)
appointmentInfo = [AppointmentInfo MR_findAllWithPredicate:predicate];
SingletonAppointments *sharedInstance = [SingletonAppointments sharedInstance]; // initialize
// if(sharedInstance.globalApptList.count > 0) // <-------------- TODO
// [sharedInstance.globalApptList removeAllObjects]; //removeAllObjects]; // clear out the old stuff
if(appointmentInfo.count > 0) {
for (AppointmentInfo *appt in appointmentInfo) {
sharedInstance.globalApptList = appointmentInfo; // move data to sharedInstance
}
}
else {
sharedInstance.globalApptList = nil;
}
// NSLog(@"\n\nsharedInstance.count: %d", sharedInstance.globalApptList.count);
// re-draw the schedule
[self.subViewData setNeedsDisplay];
}
}
This is the output from the "crash":
2013-04-17 12:58:47.942 saori[11294:c07] -[AppDelegate calendarTapNotification:]: unrecognized selector sent to instance 0x8a25c60 2013-04-17 12:58:47.943 saori[11294:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate calendarTapNotification:]: unrecognized selector sent to instance 0x8a25c60' * First throw call stack: (0x2701012 0x19efe7e 0x278c4bd 0x26f0bbc 0x26f094e 0x14b04f9 0x275b0c5 0x26b5efa 0x13e4bb2 0x7de8 0x80eb 0x966cef 0x966f02 0x944d4a 0x936698 0x2b7edf9 0x2b7ead0 0x2676bf5 0x2676962 0x26a7bb6 0x26a6f44 0x26a6e1b 0x2b7d7e3 0x2b7d668 0x933ffc 0x297d 0x28a5 0x1) libc++abi.dylib: terminate called throwing an exception
I have tried everything I know of, read the two Apple docs on NSNotifications and looked at Google and SO and found nothing)
Why doesn't it handler for notification get called? (which I suppose will lead to what is causing the crash).
Make sure you've implemented
calendarTapNotification:
notificationOfAppointmentMade:
in your AppDelegate
.