我有一个由几个 UIViewControllers 组成的菜单系统,焦点由 NavigationController 传递。这些控制器中的大多数都引用(如果不操纵)一个单例,该单例负责管理我的游戏中的课程,称为 CoursesManager <-(不是 UIViewController)。创建和编辑系统运行良好,但我一直遇到一个问题,即我丢失数据或收到一个空的(垃圾数据)课程数组,导致 CoursesManager 在尝试更新其中一个 UIViewController 中的某些标签时崩溃。这种数据丢失总是出现在一个特定“问题”(UIViewController*)ProblemViewController 的以下方法之一中:viewDidAppear、viewWillAppear、viewDidDisappear 和 viewWillDisappear。
我的理论是,当 ProblemUIViewController 尝试访问数据时,另一个 UIViewController 仍会以某种方式使用数据,这会导致数据错误。不过,我不确定如何测试或解决此问题。我对线程在 iPhone 编程中的一般工作方式也有些模糊。在我看来也可能是这种情况,因为 ProblemUIViewController 位于 NavigationController 堆栈的底部。
任何帮助将不胜感激。
编辑 这是我收到的错误消息。
-[CALayerArray isEqualToString:]:无法识别的选择器发送到实例 0x2d31a0
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray isEqualToString:]: unrecognized selector sent to instance 0x2d31a0' * First throw call stack: (0x3262288f 0x34678259 0x32625a9b 0x32624915 0x3257f650 0x3204d3f9 0x3141f 0x3123b 0x3208bb95 0x320e58af 0x3208e913 0x3208e503 0x320d88eb 0x320d8719 0x320bcbc1 0x343cb 0x3257c3fd 0x32064e07 0x32064dc3 0x32064da1 0x32064b11 0x32065449 0x3206392b 0x32063319 0x32049695 0x32048f3b 0x316d022b 0x325f6523 0x325f64c5 0x325f5313 0x325784a5 0x3257836d 0x316cf439 0x32077cd5 0x22c1 0x2280) terminate called throwing an exception(lldb)
当代码进入此函数时,将引发此异常。
-(void)viewWillAppear:(BOOL)animated { [self refreshCourses]; [自我切换编辑模式:否];}
-(void)refreshCourses { struct SCourse* 课程 = [coursesManager GetAllCourses];
for(int i = 0; i < MAX_COURSES; ++i) { // deactivate all buttons/labels courseButtons[i].hidden = YES; courseLabels[i].hidden = YES; deleteButtons[i].hidden = YES; // remake with current data if(courses[i].inUse == YES) { switch (courses[i].size) { case 1: [courseButtons[i] setImage:[UIImage imageNamed:@"sm_coursename_BG.png"] forState:UIControlStateNormal]; break; case 2: [courseButtons[i] setImage:[UIImage imageNamed:@"med_coursename_BG.png"] forState:UIControlStateNormal]; break; case 4: [courseButtons[i] setImage:[UIImage imageNamed:@"lg_coursename_BG.png"] forState:UIControlStateNormal]; break; default: break; } courseLabels[i].text = courses[i].name; // exception thrown here on second iteration of loop with 2 courses to loop through courseLabels[i].hidden = NO; courseButtons[i].hidden = NO; } // if } // for }
-(void)toggleEditMode:(BOOL)bMode {
bEditMode = bMode; if (bMode == YES) { for (int i = 0; i < MAX_COURSES; ++i) { if(courseButtons[i].hidden == NO) { struct SCourse tempCourse = [coursesManager GetCourseAtIndex:i]; courseLabels[i].text = [NSString stringWithFormat:@"(edit) %@", tempCourse.name]; deleteButtons[i].hidden = NO; } } } else // bMode == NO { for (int i = 0; i < MAX_COURSES; ++i) { if (courseButtons[i].hidden == NO) { struct SCourse tempCourse = [coursesManager GetCourseAtIndex:i]; courseLabels[i].text = tempCourse.name; } deleteButtons[i].hidden = YES; } } }
调用单例获取所有课程的方法:
-(struct SCourse*)GetAllCourses { 返回课程;}
courseManager 是我的单身人士,它拥有所有数据。