xcode NavigationLink 只工作一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/59279176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NavigationLink Works Only for Once
提问by C.Aglar
I was working on an application with login and after login there are categories listed. And under each category there are some items listed horizontally. The thing is after login, main page appears and everything is listed great. When you click on an item it goes to detailed screen but when you try to go back it just crashes. I found this flow Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`?but i could not solve my problem. Since my project become complicated, I just wanted to practice navigation in swiftui and I created a new project. By the way I downloaded the latest xcode version 11.3. I wrote a simple code as follows:
我正在开发一个登录应用程序,登录后列出了类别。在每个类别下都有一些水平列出的项目。事情是登录后,主页面出现,一切都列出来了。当您单击一个项目时,它会进入详细屏幕,但是当您尝试返回时,它就会崩溃。我发现了这个流程为什么我的 SwiftUI 应用程序在“NavigationView”中的“navigationBarItems”内放置“NavigationLink”后向后导航时会崩溃?但我无法解决我的问题。由于我的项目变得复杂,我只想在swiftui中练习导航并创建了一个新项目。顺便说一下,我下载了最新的 xcode 版本 11.3。我写了一个简单的代码如下:
NavigationView{
NavigationLink(destination: Test()) {
Text("Show Detail View")
}
.navigationBarTitle("title1")
And Test() view is as follows:
而 Test() 视图如下:
import SwiftUI
struct Test: View {
var body: some View {
Text("Hello, World!")
}
}
struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}
As you can see it is really simple. I also tried similar examples on the internet but it does not work the way it suppose to work. When I run the project, I click the navigation link and it navigates to Test() view. Then I click back button and it navigates to the main page. However, when I click the navigation link second time, nothing happens. Navigation link works only once and after that nothing happens. It does not navigate, it des not throw any error. I am new to swiftui and everything is great but the navigation. I tried many examples and suggested solutions on the internet, but nothing seems to fix my issues.
如您所见,这非常简单。我也在互联网上尝试过类似的例子,但它并没有像它想象的那样工作。当我运行该项目时,我单击导航链接并导航到 Test() 视图。然后我点击后退按钮,它导航到主页。但是,当我第二次单击导航链接时,没有任何反应。导航链接只工作一次,之后什么也没有发生。它不导航,也不抛出任何错误。我是 swiftui 的新手,除了导航之外,一切都很棒。我在互联网上尝试了许多示例和建议的解决方案,但似乎没有任何解决方案可以解决我的问题。
采纳答案by Sagun Raj Lage
[UPDATE] Feb 12, 2020 - I checked for this issue in Xcode 11.4 beta and found that this issue has been resolved.
[更新] 2020 年 2 月 12 日 - 我在 Xcode 11.4 beta 中检查了这个问题,发现这个问题已经解决。
I was getting the same issue in my project too, when I was testing it in Xcode's simulator. However, when I launched the app on a real device (iPhone X with iOS 13.3), NavigationLink
was working totally fine. So, it really does seem like Xcode's bug.
当我在 Xcode 的模拟器中测试它时,我在我的项目中也遇到了同样的问题。但是,当我在真实设备(带有 iOS 13.3 的 iPhone X)上启动该应用程序时,NavigationLink
运行正常。所以,它确实看起来像是 Xcode 的错误。
回答by Victor Kushnerov
Simulator 11.4: This issue has been fixed
模拟器 11.4:此问题已修复
You need to reset the default isActive
value in the second view.
It works on devices and emulators.
您需要isActive
在第二个视图中重置默认值。它适用于设备和模拟器。
struct NavigationViewDemo: View {
@State var isActive = false
var body: some View {
NavigationView {
VStack {
Text("View1")
NavigationLink(
destination: NavigationViewDemo_View2(isActive: $isActive),
isActive: $isActive,
label: { Button(action: { self.isActive = true }, label: { Text("click") }) })
}
}
}
}
struct NavigationViewDemo_View2: View {
@Binding var isActive: Bool
var body: some View {
Text("View2")
.navigationBarItems(leading: Button(action: { self.isActive = false }, label: { Text("Back") }))
}
}
回答by Brandon C.
Presumably this will be resolved when Apple fixes the related bug that prevents 13.3 from being selectable as a deployment target.
据推测,当 Apple 修复阻止 13.3 被选为部署目标的相关错误时,这将得到解决。
I'm experiencing the same issue as everyone else. This issue is present in simulator and preview running 13.2, but is fixed when deploying to my own device running 13.3.
我遇到了和其他人一样的问题。此问题存在于运行 13.2 的模拟器和预览版中,但在部署到我自己的运行 13.3 的设备时已修复。
回答by ozmpai
As @Александр Грабовский said its seems like a Xcode 11.3 bug, I am encountering the same problem, you must downgrade or use some workaround like custom back button as below
正如@Александр Грабовский 所说,它似乎是 Xcode 11.3 的错误,我遇到了同样的问题,您必须降级或使用一些解决方法,例如如下自定义后退按钮
struct ContentView: View {
@State private var pushed: Bool = false
var body: some View {
NavigationView {
VStack {
Button("Show Detail View") {
self.pushed.toggle()
}
NavigationLink(destination: Test(pushed: $pushed), isActive: $pushed) { EmptyView() }
}.navigationBarTitle("title1")
}
}
}
struct Test: View {
@Binding var pushed: Bool
var body: some View {
Text("Hello, World!")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: BackButton(label: "Back") {
self.pushed = false
})
}
}
struct BackButton: View {
let label: String
let closure: () -> ()
var body: some View {
Button(action: { self.closure() }) {
HStack {
Image(systemName: "chevron.left")
Text(label)
}
}
}
}