3

我在详细信息视图中切换设置时遇到问题,在切换设置后,导航立即弹回主屏幕。只有在主屏幕上使用 ScrollView 而不是 List 时才会发生这种情况。有没有人有解决方案或知道这是否是要修复的错误?我今天等待 Xcode 11 Beta 7,但这并没有解决问题。

ScrollView 的当前行为:

  1. 单击要转到详细信息屏幕的行
  2. 点击开始切换收藏设置
  3. 立即跳回主屏幕

ScrollView 的预期行为(List 的当前行为):

  1. 单击要转到详细信息屏幕的行
  2. 点击开始切换收藏设置
  3. 留在详细信息屏幕上,并能够一遍又一遍地切换最喜欢的设置
  4. 单击返回按钮返回主屏幕

    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
    
4

1 回答 1

1

可能会影响这一点的一件事是,您似乎正在破坏 forEach if 中的原始 NavigationLink favorite == false

因此,如果那是我们进入 Detail 的 NavigationLink,那么当 favourite 设置为 false 时它就会消失。那是你打算做的吗?

于 2021-06-23T16:59:17.263 回答