gpt4 book ai didi

ios - 为什么 NSNotificationCenter 发布会导致“无法识别的选择器发送到实例?”

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:09 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

确保你已经实现

calendarTapNotification:
notificationOfAppointmentMade:

在你的 AppDelegate 中。

关于ios - 为什么 NSNotificationCenter 发布会导致“无法识别的选择器发送到实例?”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16069306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com