ios - 如何以编程方式从详细 View Controller 返回到主视图 Controller ?

标签 ios iphone swift swift2 navigationcontroller

我在将数据从详细 View Controller 传递到主视图 Controller 时遇到问题。感兴趣部分的层次结构是

TabBarController -> Profile View Controller --segue--> Navigation Controller -> Detail View Controller

详细 View Controller 包含图像的 UICollectionView,每个图像都有一个点击手势识别器。我想做的是点击其中一张图像,将 indexPath.row 传递给 ProfileViewController 并以编程方式返回。

当我转到详细信息 View Controller 时,那里已经有一个“后退”按钮,因此想法是:如果用户按下“返回”按钮,则不会发生任何事情。如果用户按下图像,后退按钮会自行触发,并且配置文件中的图像会发生变化。

到目前为止,我可以点击图像,传递indexPath.row,但我无法返回到之前的 View Controller 。

我尝试过 navigationController?.popViewControllerAnimated(true)navigationController?.popToRootViewControllerAnimated(true) 但它们都不起作用(我将它们放入手势的点击功能中,导航 Controller 没有任何反应,点击功能工作正常)

当我打印以下内容时,我得到 1 navigationController.viewControllers.count

我唯一能做的就是 dismissViewControllerAnimated 但是它会从 TabBarController 转到之前的另一个 View 。

最佳答案

更新

我发现了,它与编程无关,正确的答案是使用 navigationController?.popViewControllerAnimated(true) 但是我的 View 层次结构是错误的

TabBarController -> Navigation Controller -> Profile View Controller --segue-> Detail View Controller

现在一切正常了

关于ios - 如何以编程方式从详细 View Controller 返回到主视图 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853422/

相关文章:

ios - 没有收到来自 LinkedIn Objective-C 的电子邮件

iphone - 如何在 iOS 企业帐户中创建开发人员配置文件请求?

iphone - 地址簿中记录的 ABRecordID(地址簿中插入记录的唯一 ID)

ios - SwiftUI 无法添加 onChange 事件

ios - 如何在 swift 中使用自定义的 uitableviewcell 进行 Storyboard

ios - 获取 UITableViewCell 索引以响应位于 TableView 内 TableView 中的按钮单击的正确方法?

ios - 为什么将我的 View Controller 添加到导航 Controller 似乎会干扰我的约束?

ios - 使用 GLKit 加载立方体贴图后,iOS 上 openGL ES 2.0 的正确立方体贴图坐标是多少?

iphone - 为 iPhone 应用程序创建自定义 UITabBar 真的很酷吗?

swift - 优化惰性集合