2

我有一个包含多个类的 iPad 应用程序(XCode 4.6、Storyboards、iOS 6.2)。在一个班级中,当我完成发布后,我发布了一条通知,我希望另一个班级能够收到该通知。没有发生!

这是我在 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 ];

这是我为其中一位观察员发布通知的代码:

if(aDate != nil) { // 如果用户在有效日期范围之外点击,aDate 将为 nil

    [passedData setObject:aDate forKey:@"selectedDate"];

    //  post notification CFGregorianDate has been tapped and pass selected date
    [[NSNotificationCenter defaultCenter] postNotificationName:@"calendarDateSelected" object:self userInfo:passedData];

}

这是我处理从未被调用的“calendarTapNotification”通知的代码:

- (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];

}

}

这是“崩溃”的输出:

2013-04-17 12:58:47.942 saori[11294:c07]-[AppDelegate calendarTapNotification:]:无法识别的选择器发送到实例 0x8a25c60 2013-04-17 12:58:47.943 saori[11294:c07] * 终止应用程序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:终止调用抛出异常

我已经尝试了我所知道的一切,阅读了 NSNotifications 上的两个 Apple 文档并查看了 Google 和 SO,但一无所获)

为什么不调用通知处理程序?(我想这会导致导致崩溃的原因)。

4

2 回答 2

4

确保你已经实施

calendarTapNotification:
notificationOfAppointmentMade:

在你的AppDelegate.

于 2013-04-17T20:10:46.723 回答
2

如果您还没有实现该calendarTapNotification:方法,请实现它,其次确保您在使用方法时使用该参数:

当您为通知添加观察者时,您应该能够收听它,为了收听通知,它应该被发布。确保您calendarDateSelected在代码中发布通知。我没有看到你发布的任何地方。

于 2013-04-17T20:13:48.613 回答