简体   繁体   English

SwiftUI Scrollview 打破 NavigationLink 行为

[英]SwiftUI Scrollview Breaks NavigationLink Behavior

I'm having a problem where I toggle a setting in a details view, and immediately after the toggle of the setting, the navigation pops back to the main screen.我在详细信息视图中切换设置时遇到问题,在切换设置后,导航立即弹回主屏幕。 This only happens when using a ScrollView instead of a List on the main screen.只有在主屏幕上使用 ScrollView 而不是 List 时才会发生这种情况。 Does anyone have a solution or know if this is bug that will be fixed?有没有人有解决方案或知道这是否是将被修复的错误? I waited for Xcode 11 Beta 7 today, but that has not fixed the issue.我今天等待 Xcode 11 Beta 7,但这并没有解决问题。

Current Behavior With ScrollView: ScrollView 的当前行为:

  1. Click on a row to be taken to details screen单击要进入详细信息屏幕的行
  2. Click on the start to toggle the favorite setting点击开始切换收藏夹设置
  3. Immediately jumps back to the main screen立即跳回主屏幕

Expected Behavior With ScrollView (Current Behavior With List): ScrollView 的预期行为(列表的当前行为):

  1. Click on a row to be taken to details screen单击要进入详细信息屏幕的行
  2. Click on the start to toggle the favorite setting点击开始切换收藏夹设置
  3. Stay on the details screen and be able to toggle the favorite setting over and over留在详细信息屏幕上,并能够一遍又一遍地切换收藏夹设置
  4. Click on the back button to be taken back to the main screen单击后退按钮可返回主屏幕

    import SwiftUI import Combine struct Sport: Identifiable{ var id = UUID() var name : String var isFavorite = false var school : String } final class UserData: ObservableObject { @Published var sportsData = [ Sport(name: "soccer", isFavorite: false, school: "WPI"), Sport(name: "tennis", isFavorite: true, school: "WPI"), Sport(name: "swimming", isFavorite: true, school: "WPI"), Sport(name: "running", isFavorite: true, school: "RIT"), ] } struct ContentView: View { @EnvironmentObject var userData: UserData var body: some View { NavigationView{ List{ ForEach(userData.sportsData){ sport in if sport.isFavorite{ NavigationLink(destination: DetailsView(sport: sport) ){ HStack { Text(sport.name) Spacer() Image(systemName: "star.fill") .foregroundColor(sport.isFavorite ? .yellow : .gray) } } } } } }.navigationBarTitle("Settings") } } struct DetailsView: View { @EnvironmentObject var userData: UserData var sport: Sport var sportIndex: Int { userData.sportsData.firstIndex(where: { $0.id == sport.id })! } var body: some View { ZStack { Text(sport.name).offset(x: 0, y: 100) Button(action: { self.userData.sportsData[self.sportIndex].isFavorite.toggle() print(self.sport.isFavorite) }) { Image(systemName: "star.fill") .foregroundColor(self.userData.sportsData[self.sportIndex].isFavorite ? .yellow : .gray) } }.padding(.horizontal) } } #if DEBUG struct Testing_Previews: PreviewProvider { static var previews: some View { ContentView() .environmentObject(UserData()) } } #endif

one thing that could be affecting this is it looks like you are destroying the original NavigationLink in the forEach if favorite == false .可能会影响这一点的一件事是,如果favorite == false您似乎正在破坏 forEach 中的原始 NavigationLink 。

So if that is the NavigationLink we came in on to the Detail, then it's disappearing when favorite is set to false.因此,如果那是我们进入 Detail 的 NavigationLink,那么当 favorite 设置为 false 时它就会消失。 Is that what you are intending to do?这是你打算做的吗?

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

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