我對 Xcode 很陌生,我正在嘗試在 NavigationLink 中放置一個 if 陳述句。
現在這是我的代碼:
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()
}
}
uj5u.com熱心網友回復:
假設那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)
})
uj5u.com熱心網友回復:
在您的第一個示例中,您的if陳述句可以回傳 aStartView或 a Finish。問題是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)
})
uj5u.com熱心網友回復:
問題是您沒有@State初始化StartView(). 您有 24 個變數未初始化且沒有init().
當視圖有點復雜時,XCode 總是很難處理視圖,因此您會在視圖中看到錯誤,NavigationLink因為它無法處理其內容。
您可以在每個變數上設定一個默認值,例如@State var Q1: String = "",或者創建一個init() { },但是您必須在呼叫StartView(Q1: "Hello, Q2: "World", ...).
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430429.html
標籤:if 语句 迅捷 swiftui-导航链接
