swift - 从 navigationStack 中移除 ViewController 并添加它的新实例
问题描述
我有一个嵌入了 navigationController 的 SongNamesViewController。当从列表中选择一首歌曲时,我打开 PlaySongViewController 并使用以下函数添加到 navigationController:
func openPlaySongViewController() {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let playSongViewController = storyBoard.instantiateViewController(withIdentifier: "playSongViewController")
self.navigationController?.pushViewController(playSongViewController, animated: true)
}
现在,一条消息通过远程推送通知到达。用户点击推送通知图标,然后我使用“openPlaySongViewController()”函数显示歌曲。如果另一个推送通知出现,然后我在现有 PlaySongViewController 之上显示另一个 PlaySongViewController。
电流:(NavigationController)->SongNamesViewController>PlaySongViewController->PlaySongViewController
在添加新的 PlaySongViewController 实例之前,如何删除 navigationController 上现有的 PlaySongViewController?
我尝试了以下方法,但 navigationController 上的 PlaySongViewController 并没有消失。
for viewController in self.navigationController!.viewControllers {
if viewController.isKind(of: PlaySongViewController.self) {
viewController.removeFromParent()
}
}
解决方案
topViewController
返回导航堆栈顶部的内容。pop
因此,如果需要,您应该这样做。
func removeLastControllerIfNeeded() {
guard navigationController?.topViewController is PlaySongViewController else { return }
navigationController?.popViewController(animated: true)
}
推荐阅读
- json - 动态 json 的 Golang 结构,返回空值
- python - 阅读没有标题的列
- git - 分支之间的 Git 历史差异
- sas - 如何使用数据格式 DDMMYYDw。在 Proc Import SAS MACRO
- prettier - 当我不希望它时,更漂亮地格式化一个 Vue 组件
- docker - dotnetcoresdk 容器中的“dotnet publish”命令生成没有详细信息的 EXE
- r - R,stringr,mutate(我认为) - 多个字符串中的多个部分字符串替换
- django - 尽管有 DEBUG = False,但看到 Django 调试 404 页面
- python - 如何从数据框中删除重复记录?
- javascript - JQuery附加图像未显示