Swift在准备segue后解除导航控制器

时间:2017-10-09 19:01:43

标签: ios swift uiviewcontroller uinavigationcontroller uistoryboardsegue

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

左下角的控制器是AddPhotoVC

在SelectAlbumVC中我有这段代码:

UiFac.Dispose();

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

有没有办法在调用segue准备时解除导航控制器? 我试图添加底部

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
    }
}

但它不起作用。

任何帮助都将非常感激。

谢谢!

2 个答案:

答案 0 :(得分:2)

如果我正确理解您的代码,您可以将另一个segue添加回AddPhotoVC。因此,如果您的应用程序正在运行并且您单击了" Debug View Hierarchy" (通过Xcode中的调试器控件)你可以看到你现在在原始的上面有另一个AddPhotoVC。

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

答案 1 :(得分:2)

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

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

了解推送/显示和呈现viewControllers之间的不同。

推/秀---流行。

现在---解雇。

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

使用此:

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