我有一个主要的文章列表,单击后它会链接到阅读视图控制器,在那里我使用具有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%进度指示器。

最佳答案

这里:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];

    if ([self.delegate respondsToSelector:@selector(finishedReading:)]) {
        [self.delegate finishedReadingWithPosition:self.position];
    }
}
  • viewDidDisappear:应该将相同的“did”方法传递给super,而不是viewWillDisappear: ..[super viewDidDisappear:animated];
  • 选择器finishedReading:与选择器finishedReadingWithPosition:不同。由于未在委托中实现,因此不会调用条件。

  • 错别字-还是解决方案...?

    关于ios - 试图实现一个在导航栏中按下后退按钮时发出信号的委托(delegate)-我在做什么错?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15622699/

    10-12 03:37