繁体   English   中英

准备好进行搜索后迅速关闭导航控制器

[英]Swift Dismiss navigation controller after prepare for segue

我有以下架构: 在此处输入图片说明 右上角的控制器是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上的关闭按钮时,它会再次出现(它是堆栈中的最后一个控制器)。

调用“准备segue”时,是否有办法解散导航控制器? 我尝试添加底部

self.navigationController?.popToRootViewController(animated: false)

但它不起作用。

任何帮助将不胜感激。

谢谢!

如果我正确理解了您的代码,则说明您正在将另一个序列添加回AddPhotoVC。 因此,如果您的应用程序正在运行,并且您单击了“ Debug View Hierarchy”(位于Xcode中的调试器控件下方),您将看到在原来的顶部有另一个AddPhotoVC。

与其执行从SelectPhotoVC到AddPhotoVC的Segue的选择,不如考虑执行Unwind Segue。 这样,您可以传递所需的值,并且所有先前的VC都将被关闭。

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

现在,您提到popToRootViewController不起作用,这是因为两个屏幕再次具有两个不同的UINavigationController 您应该dismiss第二个屏幕而不是将其popping ,因为您已经显示了它。

了解推送/显示和呈现viewController之间的区别。

推送/显示---弹出。

目前---解雇。

代替此self.navigationController?.popToRootViewController(animated: false)

用这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM