簡體   English   中英

prepareForSegue從未被召喚過

[英]prepareForSegue never being called

我有一個由許多單元格組成的UICollectionView。 我希望能夠點擊其中一個單元格,並在確定這是適當的操作后讓我的故事板轉向另一個視圖控制器。

我在Storyboard中創建了我的輔助視圖控制器以及segue。 在我的UICollectionView子類中,我實現了以下...

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    return NO; // So that I can determine whether or not to perform the segue based on app logic
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    // Selection logic here
    [self performSegueWithIdentifier:@"showDetailView" sender:self];
}

在這個類中,我還實現了 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender,允許我在執行segue之前設置我的詳細視圖。 它看起來像這樣:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   NSLog(@"HELLO, WORLD!!!");
}

一切似乎都井然有序,但我發現,准備prepareForSegue絕對不會被召喚。 我已經嘗試過記錄,設置斷點。 沒有跡象表明這種方法被稱為EVER。 我在這里錯過了什么? 為什么不調用此方法? 雖然它現在有一個簡單的“Hello,World”語句,但我之前有我的詳細信息視圖所依賴的代碼,它會導致異常,因為詳細視圖設置不正確。

還有什么我在這里不見了嗎? 我應該做的事情,我現在不是嗎?

你告訴iOS永遠不要與此相悖:

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    return NO; // So that I can determine whether or not to perform the segue based on app logic
}

將NO改為YES,它應該有效。 或者在里面添加邏輯,當你想要segue時至少返回YES。

如果設置了shouldPerformSegueWithIdentifier:NO則永遠不會調用PrepareForSegue shouldPerformSegueWithIdentifier:NO

確保您使用的是原型單元格(使用動態單元格),您可以在故事板中設置標識符並在代理中使用它。 我遇到了這個問題並帶我去了解一下。

static NSString *cellIdent = @"prototypeCellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];

暫無
暫無

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

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