簡體   English   中英

當呈現UINavigationController模態時,segue.destinationViewController為nil

[英]segue.destinationViewController is nil when presenting UINavigationController modal

我有一個UICollectionView ,當用戶按下一個單元格時,我使用一個故事板以模態方式呈現UINavigationController另一個視圖控制器。

- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"editBookIPad"
                                      sender:indexPath];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue
                 sender:(id)sender
{
    // Did not include code for other segues, but I check which is the current one properly
    UINavigationController *dest = segue.destinationViewController; // This is nil!
    dest.modalPresentationStyle = UIModalPresentationFormSheet;
    DetailsViewController *detailsVC = (id)[dest topViewController];
    detailsVC.stack = self.stack;
    detailsVC.editingMode = 1;
    detailsVC.bookToEdit = [self.fetchedResultsController objectAtIndexPath:sender];
    [self.collectionView deselectItemAtIndexPath:sender
                                        animated:YES];
}

故事板

現在,我的問題是segue.desinationViewController返回nil (如代碼片段中的注釋所示)。

只是為了調試,我將UINavigationController更改為另一個視圖控制器,我沒有問題。 我不知道是否從modal更改為push作為轉換樣式會有所幫助,因為它不可能推送UINavigationController (發生崩潰的情況就是這樣)。

我清理了項目和構建文件夾,然后重新啟動了我的計算機(以及Xcode)。

這是運行應用程序時的樣子:

問題截圖

在尋找類似的問題時,我發現了這一點。 大多數其他問題是關於在目標視圖控制器上設置的屬性為nil(例如這個 )。

我使用Xcode 5.1.1並將iOS 7.0作為開發目標。

EDIT1

現在我的應用程序的所有部分都出現了同樣的問題( UINavigationController以模態方式呈現)。 但是 ,它只在某些情況下發生,但每次segue.destinationViewControllernil

EDIT2

我用這個替換了prepareForSegue代碼(手動完成):

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard"
                                                     bundle:nil];
UINavigationController *navCon = [storyboard instantiateViewControllerWithIdentifier:@"AllBooksVCDetails"]; // The problematic navigation controller
navCon.modalPresentationStyle = UIModalPresentationFormSheet;
BKSBookDetailsViewController *detailsVC = (id)[navCon topViewController];
detailsVC.stack = self.stack;
detailsVC.editingMode = 1;
detailsVC.bookToEdit = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self presentViewController:navCon
                   animated:YES
                 completion:nil];
[self.collectionView deselectItemAtIndexPath:indexPath
                                    animated:YES];

這很有效。 所以我認為問題出在故事板上。

將tracecontroller嵌入到NavigationController中時遇到了同樣的問題。

 override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let destinationVC = segue.destinationViewController as! UINavigationController
        let nextViewController = destinationVC.viewControllers[0] as! SecondViewController

        nextViewController.someProperty = someValue
    }

這個鏈接幫助我:

https://www.andrewcbancroft.com/2015/06/02/access-sub-controllers-from-a-uinavigationcontroller-in-swift/

我也使用swift遇到過這個問題,但是1小時后,觀察到我沒有使用segue .destinationViewController,而是使用了發送器 .destinationViewController,它搞砸了所有東西。 你確定你使用的是segue而不是發件人嗎?

問題是在UINavigationController中嵌入UIViewController時。 S. Matsepura的答案的變體,其中“ShowView”是segue的標識符(如果你有多個):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowView" {
        if let nc = segue.destination as? UINavigationController, let vc = nc.topViewController as? MyViewController {
            vc.var = "value"
        }
    }
}

我遇到了同樣的問題,我認為問題是將viewcontroller嵌入到NavigationController中。 一旦我刪除了導航控制器,prepareForSegue按預期工作,segue.destinationViewController開始返回正確的viewController類。

只是想把它添加為信息。

所以我的問題是segue.destinationViewController將nil傳遞給self.preboardingViewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "GoToPreBoarding" {


        self.preboardingViewController = segue.destinationViewController as? PreBoardingViewController
        self.preboardingViewController?.myShiftViewController = self
    }
}

所以在swift中,我幾乎和你的一樣解決了這個問題,讓導航控制器topviewController讓我失望了一下。 這是我的解決方案:

let storyboard = UIStoryboard(name: "PreBoarding", bundle: nil)
        let navCon = storyboard.instantiateViewControllerWithIdentifier("PreBoardingNavigation") as! UINavigationController
        navCon.modalPresentationStyle = UIModalPresentationStyle.FormSheet
        self.preboardingViewController = navCon.topViewController as? PreBoardingViewController
        self.preboardingViewController?.myShiftViewController = self
        self.presentViewController(navCon, animated: true, completion: nil)

還要確保在Storyboard的Destination ViewController類中,啟用Class的Identity Inspector下的Inherit Module From Target復選框

暫無
暫無

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

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