简体   繁体   English

SwiftUI:按下 NavigationView 的后退按钮时执行代码

[英]SwiftUI: Execute code when back button for NavigationView is pressed

I have MainView, which has a navigationLink to EditProfileView.我有 MainView,它有一个到 EditProfileView 的 navigationLink。

When I press the back button on EditProfileView, I want some code to execute that updates a database.当我按下 EditProfileView 上的后退按钮时,我想要执行一些代码来更新数据库。

I would use onDisappear(), but the issue is that EditProfileView has another view ImagePickerView presented as a sheet come up, and having that view come up triggers onDisappear().我会使用 onDisappear(),但问题是 EditProfileView 有另一个视图 ImagePickerView 作为工作表呈现,并且让该视图出现触发 onDisappear()。

In other words, I want my database code to execute only when we press the back button to go from EditProfileView back to MainView.换句话说,我希望我的数据库代码仅在我们按下返回按钮从 EditProfileView 返回到 MainView 时执行 go。

Is there a way to do this?有没有办法做到这一点?

Add your own back button to replace the native one as the left button.添加您自己的后退按钮以替换原生按钮作为左按钮。 Then programmatically dismiss your view.然后以编程方式关闭您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM