swift - 导航按钮需要从最后一个视图转到第一个视图
问题描述
您好,我提供了上面的链接,以显示我要完成的工作的直观视图。因此,每个按钮都是下一个视图的延续,但最后一个按钮不是预期的。由于导航控制器,最后两个视图在顶部自动包含后退按钮,我想让这些后退按钮转到第一个视图控制器。我怎么能用 xcode 和 swift 做到这一点?
解决方案
如果要删除所有导航控制器,请使用此代码
将此代码放在按钮选择器方法中
self.navigationController!.viewControllers.removeAll()
或者
navigationController?.popToRootViewController(animated: true)
或者
如果您没有导航控制器并使用dismissViewController 方法,您仍然可以使用:
view.window?.rootViewController?.dismiss(animated: true, completion: nil)
现在是返回按钮代码:
override func viewDidLoad() {
super.viewDidLoad()
var backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Any image can be used. download back button image from any site or whatever you wanna use.
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}
func backAction() -> Void {
//here put code from above whichever you wanna use for example I'm using one
self.navigationController?.popToRootViewController(animated: true)
}
推荐阅读
- google-cloud-platform - 如何使用带有 gcloud 命令行工具的 Google Cloud Secret Manager 创建 Secret?
- javascript - 剪贴板中的 JavaScript 粘贴不起作用
- python - 如何一次增加字符串字符?
- reactjs - 反应钩子中的prevstate
- sql - 没有匹配的主键?- SQL Server 管理工作室
- selenium-webdriver - 在并行运行中使用 driver.quit()
- python - 如何使用 Python 3 在 Web 浏览器中打开网站?
- javascript - 为什么这个简单的计数器会删除附加的 div?
- javascript - 以编程方式提交表单 React
- firebase - 每次热重新加载后都会加载多个图像?