簡體   English   中英

嘗試實現一個在導航欄中按下后退按鈕時發出信號的委托-我在做什么錯?

[英]Trying to implement a delegate that signals when the back button is pressed in the navigation bar - what am I doing wrong?

我有一個主要的文章列表,單擊后它會鏈接到閱讀視圖控制器,在那里我使用具有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: 由於未在委托中實現,因此不會調用條件。

錯別字-還是解決方案...?

兩件事:

首先,在您的委托responsestoSelector檢查中,確保您正在測試正確的方法。 您在檢查中輸入了“ finishedReading:”,然后在方法調用中調用了“ finishedREadingWithPosition:”。我的猜測是,由於選擇了錯誤的選擇器,因此跳過了這一行。

接下來,確保self.position具有值,並且:我同意提到@He的[super]調用-需要對同一方法進行調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM