【发布时间】:2015-04-27 21:05:35
【问题描述】:
我在 ViewController.m 中有一个名为 getData 的方法,它在 viewDidLoad 内部调用:
-(void)getData {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"WorkoutHasExercise" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
request.resultType = NSDictionaryResultType;
request.propertiesToFetch = [NSArray arrayWithObjects:@"exerciseName", @"reps", @"sets", nil];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(workoutName = %@)", _workoutName];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count] == 0) {
} else {
[_exercises removeAllObjects];
for (int x = 0; x < [objects count]; x++) {
matches = objects[x];
[_exercises addObject:[matches valueForKey:@"exerciseName"]];
[_totalSets addObject:[matches valueForKey:@"sets"]];
[_totalReps addObject:[matches valueForKey:@"reps"]];
[_currentSets addObject:[NSNumber numberWithInteger:0]];
}
}
[_exercisesTableView reloadData];
}
我还有一个自定义 UITableViewCell,其中包含在 cellForRowAtIndexPath 中启动的两个按钮:
ActiveWorkoutCell *cell = (ActiveWorkoutCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ActiveWorkoutCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.increaseButton.tag = indexPath.row;
cell.decreaseButton.tag = indexPath.row;
在 ActiveWorkoutCell.m 中,按钮有 2 个 IBAction:
- (IBAction)decreaseSets:(id)sender {
ActiveWorkoutViewController *vc = [[ActiveWorkoutViewController alloc] init];
[vc decreaseSets:[sender tag]];
}
- (IBAction)increaseSets:(id)sender {
ActiveWorkoutViewController *vc = [[ActiveWorkoutViewController alloc] init];
[vc increaseSets:[sender tag]];
}
IBActions 在 ViewController.m 中调用这 2 个方法
-(void)increaseSets:(NSInteger)row {
[self getData];
//There will be code here to increase the value of currentSets[row]
}
-(void)decreaseSets:(NSInteger)row {
[self getData]
//Code to decrease value...
}
问题:
当从 viewDidLoad 调用 getData 时,它工作正常。 从 ActiveWorkoutCell.m 中的 IBAction 返回 ViewController.m 时会出现问题。 当我在 increaseSets 中调用 [self getData] 时,获取请求返回一个空数组。这让我感到困惑 - 代码在第一次调用时可以正常工作,但在自定义单元格操作被触发后第二次调用时根本不行。
如果有帮助,这是我的 viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
_exercises = [NSMutableArray array];
_totalSets = [NSMutableArray array];
_currentSets = [NSMutableArray array];
_totalReps = [NSMutableArray array];
_titleLabel.text = _workoutName;
_exercisesTableView.allowsSelection = NO;
[self getData];
}
_workoutName 在前一个视图控制器的 prepareForSegue 中被赋予了一个值。
【问题讨论】:
-
您的
NSMutableArray属性是否正在实例化?如果是这样,当您逐步执行代码时,它们的值是什么? -
它们在第一次调用 getData 时(从 viewDidLoad)按预期填充,但是一旦按下自定义单元格中的按钮并触发 ActiveWorkoutCell.m 中的 IBAction,它们就会变为 nil。跨度>
-
您的 IBAction 会创建 ActiveWorkOutViewController 的新副本。这些新副本不会出现,因此 viewDidLoad 不会运行(在这些副本中)。所以可变数组没有被初始化。
-
@pbasdf 你能解释一下这个问题以及我应该如何解决它吗?
-
这一行:
ActiveWorkoutViewController *vc = [[ActiveWorkoutViewController alloc] init];创建了一个新的 ViewController 实例。您需要访问现有实例。有几种方法 - 我认为最简单的方法是将delegate属性添加到自定义单元格,将delegate的值设置为cellForRowAtIndexPath中的self,然后使用self.delegate代替vc在您的IBAction 方法。
标签: ios objective-c iphone xcode core-data