例如,這是一個視圖:
struct ContentA: View {
var body: some View {
Text(“Hello”)
}
}
struct ContentB: View {
var body: some View {
Button("Lets Go!") {
// Here i wanna to move to ContentA view
}
}
}
我想要一種通過按鈕到達 ContentA 視圖的方法。
喜歡這張照片:這里
uj5u.com熱心網友回復:
使用 SwiftUI 執行此操作的最簡單方法是NavigationLink. 我已經測驗了這個解決方案的作業。您還可以將視圖顯示為作業表。我會在這里尋找該解決方案。https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-new-view-using-sheets
我相信NavigationLink解決方案也需要您擁有NavigationView。
import SwiftUI
struct ContentA: View {
var body: some View {
Text("Hello")
}
}
struct ContentB: View {
var body: some View {
NavigationView {
NavigationLink("Link to A", destination: ContentA())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentB()
}
}
uj5u.com熱心網友回復:
您可以通過對您想要訪問的特定視圖的這個簡單呼叫來完成此操作。如果您想訪問 contentAView 則只需像這樣在按鈕操作空間中呼叫它
struct ContentA: View {
var body: some View {
Text("Hello")
}
}
struct ContentB: View {
@State var showContentA = false
var body: some View {
NavigationView{
Button("Lets Go!") {
showContentA = true
}
}
.sheet(isPresented: $showContentA) {
ContentA()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/384055.html
上一篇:列舉解碼器json資料swift
