问题是:有动作(触摸内部按钮)我将对象保存到核心数据,如果一切正常,则解除控制器。但是控制器并没有解雇,我的代表也没有工作。我试图调试并发现信息有效并且保存,但委托和解雇不是因为未知原因。
这是代码: 动作:
- (IBAction)acceptDayOfWeek:(id)sender
{
managedObjectContext = [AppDelegate sharedDelegate].managedObjectContext;
int selectedString = [dayOfWeekPicker selectedRowInComponent:0];
NSDate *weekDayUnknwn = [days objectAtIndex:selectedString];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
weekDayForSave = [dateFormatter stringFromDate:weekDayUnknwn];
ReadySubject *readySubject = [NSEntityDescription insertNewObjectForEntityForName:@"ReadySubject" inManagedObjectContext:self.managedObjectContext];
readySubject.dayOfWeek = weekDayForSave;
NSError *error;
if ([managedObjectContext save:&error])
{
[self.delegate changeWeekDay];
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
NSLog(@"%@", [error localizedDescription]);
}
}
委托
#pragma mark ChangeDayOfWeekDelegate
- (void) changeWeekDay
{
[dateLabel setNeedsDisplay];
[self makingArrayOfWeekDays];
}
在代表中实现的方法
- (NSArray *)takeDayOfWeekFromCoreData
{
managedObjectContext = [AppDelegate sharedDelegate].managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *description = [NSEntityDescription entityForName:@"ReadySubject" inManagedObjectContext:managedObjectContext];
[request setEntity:description];
NSError *requestError = nil;
dayOfWeekArray = [[self.managedObjectContext executeFetchRequest:request error:&requestError] mutableCopy];
if (requestError) {
NSLog(@"%@" , [requestError localizedDescription]);
}
return dayOfWeekArray;
}
- (void)makingArrayOfWeekDays
{
[self takeDayOfWeekFromCoreData];
dayOfWeekIndexesArray = [dayOfWeekArray valueForKey:@"dayOfWeek"];
if ([dayOfWeekIndexesArray count] != 0) {
NSLog(@"%@", [dayOfWeekIndexesArray objectAtIndex:0]);
}
}
设置委托
@protocol ChangeDayOfWeekDelegate
- (void) changeWeekDay;
@end
@interface ChageDayOfWeekViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDelegate>
@property (strong, nonatomic) id <ChangeDayOfWeekDelegate> delegate;
和
#import "ChageDayOfWeekViewController.h"
@interface ScheduleViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate, MakingEventViewControllerDelegate, ChangeDayOfWeekDelegate>
答案 0 :(得分:1)
pbasdf说的是,如果代码没有执行,委托可能是nil
。要检查这一点,您可以更改操作中的代码,如下所示,然后发布结果。
if ([managedObjectContext save:&error])
{
if (self.delegate) {
NSLog(@"delegate is set");
[self.delegate changeWeekDay];
} else {
NSLog(@"delegate is not set and can not execute code");
}
NSLog(@"If I reach this part of code and I am the ViewController I should be dismissed");
[self dismissViewControllerAnimated:YES completion:nil];
}
如果这不是我不得不说你的代码片段令人恼火的原因,因为你无法看到什么属于什么,你应该总是将文件名发布在顶部。