gpt4 book ai didi

ios - 准备 segue 后 swift 关闭导航 Controller

转载 作者:行者123 更新时间:2023-11-28 10:12:13 25 4
gpt4 key购买 nike

我有以下架构: enter image description here右上角的 Controller 是SelectAlbumVC

左下角的 Controller 是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 关联的导航 Controller 不会被关闭,因此当单击 AddPhotoVC 上的关闭按钮时,SelectAlbumVC 会再次出现(它是堆栈中的最后一个 Controller )。

有没有办法在调用 prepare for segue 时关闭导航 Controller ?我试过在底部添加

self.navigationController?.popToRootViewController(animated: false)

但它不起作用。

任何帮助将不胜感激。

谢谢!

最佳答案

如果我对您的代码的理解正确,那么您正在向 AddPhotoVC 添加另一个转场。因此,如果在您的应用程序运行时单击“调试 View 层次结构”(在 Xcode 中的调试器控件下方),您会看到现在在原始 View 之上有另一个 AddPhotoVC。

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

关于ios - 准备 segue 后 swift 关闭导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com