No known instance method for selector--delegate issue?

我的梦境 提交于 2019-12-13 07:03:41

问题


I'm getting the above error, and I don't know why. The line where the error is flagged is:

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

I'm confused because there seems to be no problem with this method, immediately preceding:

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

My protocol is declared like this in the associated header file:

@protocol AddActivityViewControllerDelegate <NSObject>

-(void) addViewControllerDidSave;

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

@end

and the methods are implemented like this in the delegate object:

-(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];

}

Can someone please point out my mistake?

Edit:

There is indeed a naming problem (I was pasting in from a test app). However, I have corrected it, I believe, like so:

@protocol AddActivityViewControllerDelegate <NSObject>

-(void) AddActivityViewControllerDidSave;

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

@end

And

- (IBAction)saveButton:(UIBarButtonItem *)sender

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

And

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

And

-(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];

}

Still getting error.

Build fails even with breakpoint suggested by @Leo


回答1:


There are a couple problems with your code:

  • Your protocol defines method names without the word Activity, but your implementation defines method names with the word Activity, so those method names do not match. You can rely on completion or copy-paste to implement a protocol method and be sure the method signature matches.
  • self.delegate may simply be type id, which does not conform to a specific protocol. The compiler has no way of knowing that self.delegate can respond to your protocol methods. Change that line to:

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



来源:https://stackoverflow.com/questions/22237649/no-known-instance-method-for-selector-delegate-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!