简体   繁体   English

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

[英]Swift Dismiss navigation controller after prepare for segue

I have the following schema: 我有以下架构: 在此处输入图片说明 The controller in the upper right corner is SelectAlbumVC 右上角的控制器是SelectAlbumVC

The controller in the lower left corner is AddPhotoVC 左下角的控制器是AddPhotoVC

In SelectAlbumVC i have this code: 在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
    }
}

so basically when i click on a cell the segue will be executed and the data passed to AddPhotoVC. 因此,基本上,当我单击一个单元格时,将执行segue,并将数据传递到AddPhotoVC。 My issue is that when the segue is executed the navigation controller associated with SelectAlbumVC is not dismissed and so when clicking the dismissing button on the AddPhotoVC SelectAlbumVC is presented again (it's the last controller in the stack). 我的问题是,执行segue时,不会关闭与SelectAlbumVC关联的导航控制器,因此当单击AddPhotoVC SelectAlbumVC上的关闭按钮时,它会再次出现(它是堆栈中的最后一个控制器)。

Is there a way to dismiss the navigation controller when the prepare for segue is called? 调用“准备segue”时,是否有办法解散导航控制器? I've tried to add the the bottom 我尝试添加底部

self.navigationController?.popToRootViewController(animated: false)

but it does not work. 但它不起作用。

Any help will be really appreciated. 任何帮助将不胜感激。

Thank you! 谢谢!

If I understand your code correctly, you're adding another segue back to AddPhotoVC. 如果我正确理解了您的代码,则说明您正在将另一个序列添加回AddPhotoVC。 So if while your app was running and you clicked the "Debug View Hierarchy" (down by the debugger controls in Xcode) you'd see that you now have another AddPhotoVC on top of the original one. 因此,如果您的应用程序正在运行,并且您单击了“ Debug View Hierarchy”(位于Xcode中的调试器控件下方),您将看到在原来的顶部有另一个AddPhotoVC。

Instead of performing a segue from SelectPhotoVC to AddPhotoVC you may want to consider performing an Unwind Segue instead. 与其执行从SelectPhotoVC到AddPhotoVC的Segue的选择,不如考虑执行Unwind Segue。 This way you could pass the values you want and all the previous VCs would be dismissed. 这样,您可以传递所需的值,并且所有先前的VC都将被关闭。

You are using (multiple) two UINavigationController . 您正在使用(多个)两个UINavigationController That means you are presenting the second screen instead of pushing it. 这意味着您要presenting第二个屏幕而不是pushing它。

Now, you mentioned that popToRootViewController does not work, that's because again the two screens have two different UINavigationController . 现在,您提到popToRootViewController不起作用,这是因为两个屏幕再次具有两个不同的UINavigationController You should dismiss the second screen rather than popping it, because you presented it. 您应该dismiss第二个屏幕而不是将其popping ,因为您已经显示了它。

Know the different between pushing/show and presenting viewControllers. 了解推送/显示和呈现viewController之间的区别。

Push/Show --- Pop. 推送/显示---弹出。

Present --- Dismiss. 目前---解雇。

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

Use this: 用这个:

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