如何解决TabView 导致预览崩溃
我目前正在尝试在我的应用中制作特色游戏部分。我在 SwiftUI 中使用 TabView 来执行此操作,但遇到了预览崩溃的问题。我没有收到任何错误,也无法实时运行它。下面是导致预览崩溃的视图代码。
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 的 observable 对象。任何帮助将不胜感激。
更新:我创建了一个更简单的示例,它仍然会产生错误。在这种情况下,除了使用的数组是可观察对象之外,所有内容都是通用的。切换 ForEach 中使用的数据结构修复了它,但我想了解为什么我的可观察对象在这里不起作用。
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))
}
}
解决方法
请看下面的代码。
我将下面的部分修复为简单然后它就可以工作了。
也许这有问题。
ForEach(featuredGames.games.results) { game in
NavigationLink(destination: NavigationLazyView(GameDetailsView(gameDetailsQuery: GameQuery(gameName: game.slug)))){
GameItem(game: game)
}
}
工作代码(我自己做了一个简单的 GameQuery observableObject)
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())
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。