我有一个主要的文章列表,单击一篇文章后,它会转到阅读视图控制器,在那里我使用具有 NSNumber 属性的视图控制器来跟踪用户的阅读进度。当他们按下后退按钮时,我想将此位置更新回根视图控制器(以便我可以向他们展示他们的进度),但我的代表似乎没有工作。
在阅读视图的 .h 文件中:
@property (nonatomic, weak) id<UpdateProgressDelegate> delegate;
...
@protocol UpdateProgressDelegate <NSObject>
@required
- (void)finishedReadingWithPosition:(NSNumber *)position;
@end
在 .m 文件中:
- (void)viewDidDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
if ([self.delegate respondsToSelector:@selector(finishedReading:)]) {
[self.delegate finishedReadingWithPosition:self.position];
}
}
在我的根视图中(注意它确实实现了协议):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ReadBasicArticleSegue"] || [segue.identifier isEqualToString:@"ReadFullArticleSegue"]) {
ReadingViewController *destination = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
self.rowOfLastSelectedCell = @(indexPath.row);
Article *article = self.articles[[self.rowOfLastSelectedCell intValue]];
// Set ReadingViewController's variables so the selected article can be read
destination.textToRead = [article.body componentsSeparatedByString:@" "];
destination.wordsPerMinute = @(1500);
destination.numberOfWordsShown = @(3);
destination.delegate = self;
}
}
和...
- (void)finishedReadingWithPosition:(NSNumber *)position {
Article *article = [self.articles objectAtIndex:[self.rowOfLastSelectedCell intValue]];
article.position = position;
[self.tableView reloadData];
}
我只是不明白我做错了什么。当我按下后退按钮时,根视图控制器仍然有一个 0% 的进度指示器。