没有已知的选择器实例方法 - 委托问题?

时间:2014-03-06 22:36:30

标签: ios delegates protocols

我收到了上述错误,我不知道为什么。标记错误的行是:

- (IBAction)cancelButton:(UIBarButtonItem *)sender
{    
    [self.delegate addActivityViewControllerDidCancel:self.thisActivity];
}

我很困惑,因为这个方法似乎没有问题,紧接在前面:

- (IBAction)saveButton:(UIBarButtonItem *)sender
...          
    [self.delegate addViewControllerDidSave];
}
...       
}

我的协议在关联的头文件中声明如下:

@protocol AddActivityViewControllerDelegate <NSObject>

-(void) addViewControllerDidSave;

-(void) addViewControllerDidCancel:(WMDGActivity *) activityToDelete;

@end

并且这些方法在委托对象中实现如下:

-(void) addActivityViewControllerDidSave
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [localContext MR_saveToPersistentStoreAndWait];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

-(void) addActivityViewControllerDidCancel:(WMDGActivity *) activityToDelete
{
    [activityToDelete MR_deleteEntity];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

有人可以指出我的错误吗?

修改

确实存在命名问题(我从测试应用程序中粘贴)。但是,我相信,我已经纠正了这个问题:

@protocol AddActivityViewControllerDelegate <NSObject>

-(void) AddActivityViewControllerDidSave;

-(void) AddActivityViewControllerDidCancel:(WMDGActivity *) activityToDelete;

@end

并且

- (IBAction)saveButton:(UIBarButtonItem *)sender

{
...
    [self.delegate AddActivityViewControllerDidSave];
...
}

- (IBAction)cancelButton:(UIBarButtonItem *)sender
{    
    [self.delegate addActivityViewControllerDidCancel:self.thisActivity];
}

-(void) addActivityViewControllerDidSave
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [localContext MR_saveToPersistentStoreAndWait];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

-(void) addActivityViewControllerDidCancel:(WMDGActivity *) activityToDelete
{
    [activityToDelete MR_deleteEntity];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

仍然出现错误。

即使@Leo建议使用断点

,构建也会失败

1 个答案:

答案 0 :(得分:4)

您的代码存在一些问题:

  • 您的协议定义的方法名称没有单词活动,但您的实现使用单词活动定义方法名称,因此这些方法名称不匹配。您可以依赖完成或复制粘贴来实现协议方法,并确保方法签名匹配。
  • self.delegate可能只是类型id,不符合特定协议。编译器无法知道self.delegate可以响应您的协议方法。将该行更改为:

    [(id<AddActivityViewControllerDelegate>)self.delegate addActivityViewControllerDidCancel:self.thisActivity];