'() -> ()'型不符合'View'



我正在尝试使用 Xcode 将if语句放入 NavigationLink 中。

这是我的代码:

NavigationLink(destination: {
if location.name == "Firm of the Future" {
StartView()                   
} else {
Finish()
}
}, label: {
Text("Leer meer")
.font(.headline)
.frame(width: 125, height: 35)
})

使用此代码,我收到错误:

类型"() -> ()"不能符合"视图">

但是当我使用此代码时:

NavigationLink(destination: {

if location.name == "Firm of the Future" {
Text("Screen 1")                            
} else {
Text("Screen 2")
}
}, label: {
Text("Leer meer")
.font(.headline)
.frame(width: 125, height: 35)
})

它有效。

我在第一个代码中做错了什么?

这是视图的代码: 我正在使用多个绑定。

import SwiftUI
struct StartView: View {

@State var Q1: String
@State var Q2: String
@State var Q3: String
@State var Q4: String
@State var Q5: String
@State var Q6: String
@State var Q7: String
@State var Q8: String
@State var theAnswer1: String
@State var theAnswer2: String
@State var theAnswer3: String
@State var theAnswer4: String
@State var theAnswer5: String
@State var theAnswer6: String
@State var theAnswer7: String
@State var theAnswer8: String
@State var AnsBool1: Bool
@State var AnsBool2: Bool
@State var AnsBool3: Bool
@State var AnsBool4: Bool
@State var AnsBool5: Bool
@State var AnsBool6: Bool
@State var AnsBool7: Bool
@State var AnsBool8: Bool


var body: some View {
ZStack{
Image("WolvenBack")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()

Image("FEClogokleur")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 70, height: 70, alignment: .leading)
.position(x: 370, y: -70)





VStack{

Spacer()


Text("De klim naar")
.font(.largeTitle)
.foregroundColor(.white)
.shadow(color: .black, radius: 10, x: 10, y: 10)

Text("het Wolvennest")
.font(.largeTitle)
.foregroundColor(.white)
.shadow(color: .black, radius: 10, x: 10, y: 10)
Spacer()


NavigationLink(
destination: FietsStap1(Q1: $Q1, Q2: Q2, Q3: Q3, Q4: Q4, Q5: Q5, Q6: Q6, Q7: Q7, Q8: Q8, theAnswer1: $theAnswer1, theAnswer2: $theAnswer2, theAnswer3: $theAnswer3, theAnswer4: $theAnswer4, theAnswer5: $theAnswer5, theAnswer6: $theAnswer6, theAnswer7: $theAnswer7, theAnswer8: $theAnswer8, AnsBool1: $AnsBool1, AnsBool2: $AnsBool2, AnsBool3: $AnsBool3,AnsBool4: $AnsBool4, AnsBool5: $AnsBool5, AnsBool6: $AnsBool6,AnsBool7: $AnsBool7,AnsBool8: $AnsBool8),

label: {
Circle()
.fill(Color(red: 0.739, green: 0.818, blue: 0.022))
.frame(width: 120, height: 200, alignment: .bottom)
.padding(10)
.shadow(color: .black, radius: 10, x: 10, y: 10)
.overlay(
Text("Start")
.font(.largeTitle)
.foregroundColor(.black)

)}
)}

}


}
}



struct StartView_Previews: PreviewProvider {
@State static var Q1 = String().self
@State static var Q2 = String().self
@State static var Q3 = String().self
@State static var Q4 = String().self
@State static var Q5 = String().self
@State static var Q6 = String().self
@State static var Q7 = String().self
@State static var Q8 = String().self
@State static var theAnswer1 = String().self
@State static var theAnswer2 = String().self
@State static var theAnswer3 = String().self
@State static var theAnswer4 = String().self
@State static var theAnswer5 = String().self
@State static var theAnswer6 = String().self
@State static var theAnswer7 = String().self
@State static var theAnswer8 = String().self
@State static var AnsBool1 = Bool().self
@State static var AnsBool2 = Bool().self
@State static var AnsBool3 = Bool().self
@State static var AnsBool4 = Bool().self
@State static var AnsBool5 = Bool().self
@State static var AnsBool6 = Bool().self
@State static var AnsBool7 = Bool().self
@State static var AnsBool8 = Bool().self

static var previews: some View {
Group {
NavigationView {
StartView(Q1: Q1, Q2: Q2, Q3: Q3, Q4: Q4, Q5: Q5, Q6: Q6, Q7: Q7, Q8: Q8, theAnswer1: theAnswer1, theAnswer2: theAnswer2, theAnswer3: theAnswer3, theAnswer4: theAnswer4, theAnswer5: theAnswer5, theAnswer6: theAnswer6, theAnswer7: theAnswer7, theAnswer8: theAnswer8, AnsBool1: AnsBool1, AnsBool2:AnsBool2,AnsBool3: AnsBool3,AnsBool4: AnsBool4, AnsBool5: AnsBool5, AnsBool6: AnsBool6,AnsBool7: AnsBool7,AnsBool8: AnsBool8)
}
}
}
}

完成视图

import SwiftUI
struct Finish: View {
var body: some View {

ZStack {

Image("WolvenBack")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
.navigationBarHidden(true)
Image("FEClogokleur")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 70, height: 70, alignment: .leading)
.position(x: 410, y: 40)



VStack{
Text("")
.padding(.bottom, 140)

ScrollView {
Text("Gefeliciteerd! Je hebt de puzzel opgelost.")
.frame(width: 300)
.foregroundColor(.white)
.font(.title)
.padding(.vertical,10)
.shadow(color: .black, radius: 10, x: 10, y: 10)

Text("Je bent nu expert op het gebied van de wolf!")
.frame(width: 300)
.foregroundColor(.white)
.font(.title)
.padding(.bottom, 50)
.shadow(color: .black, radius: 10, x: 10, y: 10)

Image("Trophy")
.resizable()
.frame(width: 300, height: 250, alignment: .center)
.shadow(color: .black, radius: 10, x: 10, y: 10)


}
}
}
}
}
struct Finish_Previews: PreviewProvider {
static var previews: some View {
Finish()
}
}

在第一个示例中,if语句可以返回StartViewFinish。麻烦的是destination:块必须返回一个具体类型- 也就是说,始终是符合View协议的相同类型的对象。第二个示例有效,因为无论采用什么路径通过if语句,它都将始终返回Text视图。

好消息是,您可以将if语句包装在一个Group中 - 一个语义中立的视图,可以包含多种类型的视图,但将以destination始终接收相同类型视图的方式包装它们:

NavigationLink(destination: {
Group {
if location.name == "Firm of the Future" {
StartView()                   
} else {
Finish()
}
}
}, label: {
Text("Leer meer")
.font(.headline)
.frame(width: 125, height: 35)
})

问题是您没有初始化StartView()的所有@State变量。您有 24 个变量未初始化,也没有init()

当视图有点复杂时,XCode 总是很难处理视图,因此您会在NavigationLink中看到错误,因为它无法处理其内容。

要么为每个变量放置一个默认值,如@State var Q1: String = "",要么创建一个init() { },但随后您必须在调用StartView(Q1: "Hello, Q2: "World", ...)时提供值。

假设Finish是一个视图...您需要为导航链接提供单个视图而不是关闭,因此最简单的解决方法是使用Group,例如

NavigationLink(destination: Group {       // << here !!

if location.name == "Firm of the Future" {
StartView()

} else {
Finish()
}
}, label: {
Text("Leer meer")
.font(.headline)
.frame(width: 125, height: 35)
})

最新更新