【问题标题】:Swift Dismiss navigation controller after prepare for segue准备转场后迅速关闭导航控制器
【发布时间】:2018-03-21 01:37:13
【问题描述】:

我有以下架构: 右上角的控制器是SelectAlbumVC

左下角的控制器是AddPhotoVC

在 SelectAlbumVC 中我有这个代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let destination = segue.destination as? AddPhotoPostVC
        else { fatalError("unexpected view controller for segue") }
    guard let cell = sender as? AlbumListCells else { fatalError("unexpected cell for segue") }

    switch SegueIdentifier(rawValue: segue.identifier!)! {
    case .showAllPhotos:
        destination.fetchResult = allPhotos
        destination.headerTitleBtnString = cell.allPhotoTitle.text!
    case .showCollection:
        // get the asset collection for the selected row
        let indexPath = tableView.indexPath(for: cell)!
        let collection: PHCollection
        switch Section(rawValue: indexPath.section)! {
        case .smartAlbums:
            collection = smartAlbums.object(at: indexPath.row)
        case .userCollections:
            collection = userCollections.object(at: indexPath.row)
        default: return // not reached; all photos section already handled by other segue
        }

        // configure the view controller with the asset collection
        guard let assetCollection = collection as? PHAssetCollection
            else { fatalError("expected asset collection") }
        destination.fetchResult = PHAsset.fetchAssets(in: assetCollection, options: nil)
        destination.assetCollection = assetCollection
        destination.headerTitleBtnString = cell.collectionTitle.text!
        destination.isComingFromSelectAlbum = true
    }
}

所以基本上当我单击一个单元格时,segue 将被执行并将数据传递给 AddPhotoVC。 我的问题是,当执行 segue 时,与 SelectAlbumVC 关联的导航控制器不会被解除,因此当单击 AddPhotoVC 上的解除按钮时 SelectAlbumVC 会再次出现(它是堆栈中的最后一个控制器)。

有没有办法在调用 prepare for segue 时关闭导航控制器? 我已经尝试添加底部

self.navigationController?.popToRootViewController(animated: false)

但它不起作用。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: ios swift uiviewcontroller uinavigationcontroller uistoryboardsegue


    【解决方案1】:

    如果我正确理解了您的代码,那么您正在向 AddPhotoVC 添加另一个 segue。因此,如果在您的应用程序运行时单击“Debug View Hierarchy”(在 Xcode 中的调试器控件下方),您会看到现在在原来的顶部有另一个 AddPhotoVC。

    您可能需要考虑执行 Unwind Segue,而不是执行从 SelectPhotoVC 到 AddPhotoVC 的转场。这样你就可以传递你想要的值,并且所有以前的 VC 都将被解雇。

    【讨论】:

    • 如果我打开“Debug View Hierarchy”,我会看到一个 NavivgationController 和 SelectAlbumVC。不确定 unwind segue,因为我是 swift 新手,而且我从未见过它们,但如果这是唯一的解决方案,我会研究它并尝试找出答案......谢谢......
    • @Marco 如果你有这个项目的版本,你可以把它扔到 GitHub 上,我可以告诉你如何连接它。
    • 这里是链接...github.com/coty10/photo 是照片部分的演示应用程序。谢谢!我真的很感激......我会研究它以更好地学习它......
    【解决方案2】:

    您正在使用(多个)两个UINavigationController。这意味着你是presenting 第二个屏幕而不是pushing 它。

    现在,您提到popToRootViewController 不起作用,那是因为两个屏幕又有两个不同的UINavigationController。你应该dismiss第二个屏幕而不是popping它,因为你展示了它。

    了解推送/显示和呈现视图控制器之间的区别。

    推送/显示 --- 弹出。

    现在 --- 解雇。

    而不是这个self.navigationController?.popToRootViewController(animated: false)

    使用这个:

    self.navigationController?.dismiss(animated: true, completion: {
        // completion, do something or make it nil.
    })
    

    【讨论】:

    • 你的意思是把所有的 segue 代码放在完成处理程序中?如果我这样做,似乎数据没有通过。我更新view中的数据会出现在addPhotoVc里面
    • @Marco 如果您从 AddPhotoVC 调用 self.navigationController?.dismiss,它将解散回显示 SelectAlbumVC 之前处于活动状态的任何内容
    • @PEEJWEEJ 这是 SelectAlbumVC,因为它在 segue 之后没有被解雇......
    • Marco,希望 Causaelity 可以帮助您,因为我今天没有时间研究您的项目。但事情是这样的,您不需要使用 segue 将第二个屏幕连接回您的第一个屏幕。如果明天我有时间,而你还没有想好要做什么,请告诉我。
    • @Glenn thx... 我已经尝试过并且实际上解决了它:) 我已经在 github 存储库中添加了代码(其他评论中的链接)以及工作代码...所以如果有人需要他们可以下载。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2018-05-01
    • 2016-06-16
    • 1970-01-01
    相关资源
    最近更新 更多