簡體   English   中英

解散UINavigationController並呈現另一個UINavigationController

[英]Dismissing a UINavigationController and Presenting another UINavigationController

我有一個嵌入在UINavigationController的兩個視圖控制器, ViewControllerAViewControllerB ViewControllerA有一個顯示圖像的UICollectionView

當選擇位於索引0的攝像機圖標和UICollectionViewCell ,它應該關閉ViewControllerA並顯示ViewControllerB 此外,還有一個ViewController以模態方式呈現ViewControllerA

這就是我所做的,但它只解除了ViewControllerA ,沒有任何反應

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("cameraViewController") as! UINavigationController
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
            self.presentingViewController?.presentViewController(controller, animated: true, completion: nil)
        })

當視圖控制器被解雇時,我的猜測是self已經解除分配,所以self.presentingViewController? 也變得nil 在解雇操作發生之前,我會創建一個強大的引用來呈現視圖控制器:

let presentingViewController = self.presentingViewController
self.dismissViewControllerAnimated(true) { completed in
  if completed {
    presentingViewController?.presentViewController(controller, animated: true, completion: nil)
  }
}

另外,確保self.presentingViewController首先不是nil

沒有發生任何事情的原因是因為self.presentingViewController? 沒有。

相反,你使用的是self.presentingViewController! 在展開可選值時會出現錯誤,指出意外發現nil ,應該始終避免。

你使用self.presentingViewController?是正確的self.presentingViewController? 而不是self.presentingViewController! ,因為它更安全,但為了解決這個問題,你必須確保self.presentingViewController? 不是零。

let presenting = self.presentingViewController

self.dismissViewControllerAnimated(true, completion: {() -> Void in
    presenting?.presentViewController(controller, animated: true, completion: nil)
})

暫無
暫無

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

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