delegate method never called
first of all here is my code:
@protocol SearchTableViewControllerDelegate
@required
- (void)didSelectFileCardsContent:(FileCardsContent*)content;
@end
@interface SearchTableViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>{
id <SearchTableViewControllerDelegate> delegate;
...
}
@property (nonatomic, assign) id <SearchTableViewControllerDelegate> delegate;
...
@end
---------------------------------------------------
@implementation SearchTableViewController
@synthesize ... delegate;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FileCardsContent *fileCardToShow = [self.tableData objectAtIndex:indexPath.row];
[self.delegate didSelectFileCardsContent:fileCardToShow];
}
...
@end
---------------------------------------------------
@interface FileCardsViewCotroller : UIViewController <SearchTableViewControllerDelegate>{
...
@end
---------------------------------------------------
@implementation FileCardsViewCotroller
...
- (void)didSelectFileCardsContent:(FileCardsContent*)content{
[managedFileCards insertSingleContent:content];
self.currentFileCardsContent = content;
questionView1.text = currentFileCardsC开发者_JAVA技巧ontent.question;
}
@end
My problem is - (void)didSelectFileCardsContent:(FileCardsContent*)content in FileCardsViewCotroller is never called. There are no compiler warnings or errors. I`m not familiar with protocols and delegates so is suggest there is a conceptually problem. Anny ideas?
first it's good practice to check whether the method exists/is implemented by the delegate
if ([delegate respondsToSelector:@selector(didSelectFileCardsContent:)])
[delegate didSelectFileCardsContent:fileCardToShow];
don't use self.property when you're calling it in its own class. it executes unnecessary commands when you can just call it directly.
make sure you implemented the protocol in the delegate and implemented the method. make sure you set the delegate to self.
You just aren't assigning your delegate to anything so the message is sent to nil.
精彩评论