适用于 iOS 14.5 的 SwiftUI NavigationLink 不起作用 [英] SwiftUI NavigationLink for iOS 14.5 not working

查看:87
本文介绍了适用于 iOS 14.5 的 SwiftUI NavigationLink 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 12.4 中有以下代码运行良好

ScrollView(.horizo​​ntal, showsIndicators: false) {LazyHGrid(行:行,间距:0){堆栈{如果(类型==测验"){NavigationLink(destination: Quiz(id: quiz.id)) {VStack(对齐:.领先){文本(测验").font(.headline).foregroundColor(.white).padding(.top, 8).填充(.领先)}.background(颜色.绿色).cornerRadius(12).shadow(颜色:.green,半径:3,x:0.0,y:0.0)}} 别的 {NavigationLink(目的地:Survey(id:survey.id)){VStack(对齐:.领先){文本(调查").font(.headline).foregroundColor(.white).padding(.top, 8).填充(.领先)}.background(颜色.绿色).cornerRadius(12).shadow(颜色:.green,半径:3,x:0.0,y:0.0)}}//万一如果(类型==测验"){NavigationLink(destination: QuizResults(id: quiz.id)) {VStack(对齐:.领先){文本(测验结果").font(.headline).foregroundColor(.white).padding(.top, 8).填充(.领先)}.background(Color.blue).cornerRadius(12).shadow(颜色:.blue,半径:3,x:0.0,y:0.0)}} 别的 {NavigationLink(目的地:SurveyResults(id:survey.id)){VStack(对齐:.领先){文本(调查结果").font(.headline).foregroundColor(.white).padding(.top, 8).填充(.领先)}.background(Color.blue).cornerRadius(12).shadow(颜色:.blue,半径:3,x:0.0,y:0.0)}}}.padding([.leading, .trailing], 25)}.frame(高度:100)

我刚刚将 Xcode 更新到 12.5,但上述内容不再有效.

它在 12.4 中运行良好!?

现在,当我单击测验"元素时,它开始转换到测验视图,该视图显示它但立即关闭视图,我又回到了详细信息视图!?

有人可以看到我做错了什么,为什么现在根据 12.5 的更新停止工作?

更新

我将代码细化为最小的可重现形式.似乎正在发生的是我有两个或更多 NavigationLink 的集合.

第一个是将用户导航到测验或调查的集合,if 语句将用户引导到要填写的正确视图.

12.5 中的问题在于,用户可以单击以查看测验或调查的整体结果的第二组在第一次导航后直接不起作用.

就像我之前说的,它在 12.4 中运行良好,但似乎 12.5 不同意.有人可以提供一种更好的方式让用户点击某个元素来填写测验或调查或查看测验或调查的结果吗?

解决方案

我遇到了完全相同的问题,在 Xcode 12.4 中一切正常.

https://developer.apple.com/forums/thread/677333

我尝试关注此线程,它可能会起作用,但在某些情况下,我仍然有此错误.

<块引用>

 NavigationLink(destination: EmptyView()) {空视图()}

显然,您可以将这 3 行代码放在您的 NavigationLink 附近...如果有人得到更好的答案,我将不胜感激!

I had the following code in Xcode 12.4 that worked perfectly

ScrollView(.horizontal, showsIndicators: false) {
        
    LazyHGrid(rows: rows, spacing: 0) {
            
        HStack {
                        
            if (type == "Quiz") {
                        
                NavigationLink(destination: Quiz(id: quiz.id)) {
                                
                    VStack(alignment: .leading) {
                                    
                        Text("Quiz")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding(.top, 8)
                            .padding(.leading)
     
                    }
                    .background(Color.green)
                    .cornerRadius(12)
                    .shadow(color: .green, radius: 3, x: 0.0, y: 0.0)
                            
                }
                            
            } else {
                            
                NavigationLink(destination: Survey(id: survey.id)) {
                                
                    VStack(alignment: .leading) {
                                    
                        Text("Survey")
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding(.top, 8)
                            .padding(.leading)
                                    
                    }
                    .background(Color.green)
                    .cornerRadius(12)
                    .shadow(color: .green, radius: 3, x: 0.0, y: 0.0)
                            
                }
                            
           } // End If
                    
           if (type == "Quiz") {
                        
               NavigationLink(destination: QuizResults(id: quiz.id)) {
                                
                   VStack(alignment: .leading) {
                            
                       Text("Quiz Results")
                           .font(.headline)
                           .foregroundColor(.white)
                           .padding(.top, 8)
                           .padding(.leading) 
                        
                   }
                   .background(Color.blue)
                   .cornerRadius(12)
                   .shadow(color: .blue, radius: 3, x: 0.0, y: 0.0)
                                
               }
                            
           } else {
                                
               NavigationLink(destination: SurveyResults(id: survey.id)) {
                                    
                   VStack(alignment: .leading) {
                                
                       Text("Survey Results")
                           .font(.headline)
                           .foregroundColor(.white)
                           .padding(.top, 8)
                           .padding(.leading)
                            
                   }
                   .background(Color.blue)
                   .cornerRadius(12)
                   .shadow(color: .blue, radius: 3, x: 0.0, y: 0.0)
                                    
              }
                                
          } 
                    
      }
      .padding([.leading, .trailing], 25)
            
}
.frame(height: 100)

I just updated Xcode to 12.5 and the above does not work any more.

It was working fine in 12.4!?

Now when I click the 'Quiz' element, it starts the transition to the Quiz View which is displays it but immediately closes the view and I'm back in the Detail View!?

Can someone see what I am doing wrong, and why now based on the update to 12.5 this stopped working?

UPDATE

I refined the code to the minimal possible reproducible form. What seems to be happening is that I have two or more NavigationLinks sets.

the first is the set to navigate the user to either the Quiz or Survey which the if statement addresses the user to the correct view to fill in.

Where the issue is in 12.5 is that the second set where the user can click to go see the overall results of the Quiz or Survey does not work when it's directly after the first navigation.

Like I said before hand it worked perfectly in 12.4 but seems like 12.5 does not agree with it. Can someone offer a better way for the user to click an element to either go fill in a quiz or survey or go see the results of a quiz or survey?

解决方案

I got exactly the same problem, everything works fine with Xcode 12.4.

https://developer.apple.com/forums/thread/677333

I try to following this thread, it might work but on some case, I still have this bug.

       NavigationLink(destination: EmptyView()) {
           EmptyView()
       }

Apparently, you can put this 3 lines of code close to your NavigationLink... If someone got a better answer I will really appreciate it !

这篇关于适用于 iOS 14.5 的 SwiftUI NavigationLink 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆