首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabView导致预览崩溃

TabView导致预览崩溃
EN

Stack Overflow用户
提问于 2021-02-20 05:18:43
回答 1查看 112关注 0票数 0

我目前正在尝试在我的应用程序中创建一个特色游戏部分。我正在使用SwiftUI中的TabView来做这件事,但是遇到了一个预览从它崩溃的问题。我没有得到任何错误,也无法运行它的现场。下面是导致预览崩溃的视图的代码。

代码语言:javascript
复制
import SwiftUI

struct FeaturedGamesView: View {
    var numberOfImages: Int
    @ObservedObject var featuredGames: GameQuery
    @State private var currentIndex: Int = 0
    
    private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
    
    
    var body: some View {
     
        VStack(alignment: .leading) {
            Text("Featured")
                .font(.headline)
                .padding(.leading, 15)
                              .padding(.top, 1)
                HStack(alignment: .top, spacing: 0) {
                          TabView() {
                                ForEach(featuredGames.games.results) { game in
                                    NavigationLink(destination: NavigationLazyView(GameDetailsView(gameDetailsQuery: GameQuery(gameName: game.slug)))){
                                        GameItem(game: game)
                                    }
                                  }
                              }
                              .offset(x: (CGFloat(self.currentIndex) * -185), y: 0)
                              .animation(.spring())
                              .onReceive(self.timer) { _ in
                                  self.currentIndex = (self.currentIndex + 1) % 19
                              }
                              
                          }
                          .frame(height: 200)
                          .tabViewStyle(PageTabViewStyle())
                          .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
                          
                      
                  }
    }
}



struct FeaturedGamesView_Previews: PreviewProvider {
    static var previews: some View {
        FeaturedGamesView(numberOfImages: 3, featuredGames: GameQuery(gameCategory: GameCategory.featured))
    }
}

从我的一些实验来看,它似乎与我用来获取数据数组以填充tabview的可观察对象不同。任何帮助都将不胜感激。

更新:我创建了一个更简单的示例,它仍然会产生错误。在这种情况下,除了所使用的数组是一个可观察的对象之外,其他所有东西都是通用的。换掉ForEach中使用的数据结构可以修复它,但我希望理解为什么我的observable对象在这里不能工作。

代码语言:javascript
复制
import SwiftUI

struct Test: View {
    @State private var selection = 0
    @ObservedObject var featuredGames: GameQuery
     
        var body: some View {
            VStack(alignment: .leading) {
                Text("Featured")
                    .font(.headline)
                    .padding(.leading, 15)
                                  .padding(.top, 1)
                HStack {
                    TabView() {
                        ForEach(featuredGames.games.results) { game in
                            Text("Hi")
                        }
                    }.frame(height: 170)
                    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
                    .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
                    
                }
            }
     
        }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test(featuredGames: GameQuery(gameCategory: GameCategory.featured))
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-02-20 09:40:58

请看下面的代码。

我修复了下面的部分,简单,然后它就可以工作了。

也许这有个问题。

代码语言:javascript
复制
ForEach(featuredGames.games.results) { game in
                                NavigationLink(destination: NavigationLazyView(GameDetailsView(gameDetailsQuery: GameQuery(gameName: game.slug)))){
                                    GameItem(game: game)
                                }
                              }

工作代码(我让我的self self simple GameQuery observableObject)

代码语言:javascript
复制
import SwiftUI

struct FeaturedGamesView: View {
    var numberOfImages: Int
    @ObservedObject var featuredGames: GameQuery
    @State private var currentIndex: Int = 0
    
    private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()


var body: some View {
 
    VStack(alignment: .leading) {
        Text("Featured")
            .font(.headline)
            .padding(.leading, 15)
                          .padding(.top, 1)
            HStack(alignment: .top, spacing: 0) {
                      TabView() {
                        ForEach(featuredGames.results, id:\.self) { game in
                                NavigationLink(destination: VStack {}){
                                    Text("Hello")
                                }
                              }
                          }
                          .offset(x: (CGFloat(self.currentIndex) * -185), y: 0)
                          .animation(.spring())
                          .onReceive(self.timer) { _ in
                              self.currentIndex = (self.currentIndex + 1) % 19
                          }
                          
                      }
                      .frame(height: 200)
                      .tabViewStyle(PageTabViewStyle())
                  

    .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
                          
                      
                  }
    }
}

final class GameQuery: ObservableObject {
    var gameCagetory: Int = 0
    var results: [Int] = [1,2,3]
}

struct FeaturedGamesView_Previews: PreviewProvider {
    static var previews: some View {
        FeaturedGamesView(numberOfImages: 3, featuredGames: GameQuery())
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66285117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档