在下面的代碼中,我有一個串列,在每一行中有一個按鈕。我想要的是,當按鈕被點擊時,能夠將選定的Fruit從ContentView傳遞到SecondView。現在我可以看到SecondView,但它是空白的,里面沒有任何文字,換句話說,我沒有看到文字,Second View: [水果名稱]。請注意,我不能使用NavigationLink(destination:),因為我已經用它來在點選行的時候轉到一個細節視圖。
當每一行的按鈕被點擊時,我怎樣才能將所選的水果傳遞給第二視圖?
水果物件
struct Fruit。可識別的{
var id = UUID()
var name:String()
}
內容視圖:
struct ContentView。View {
@State private var secondViewIsPresented = false
var fruits = [Fruit(name: "Apple"), Fruit(name: "Orange") ]
var body。some View {
List{
ForEach(fruits){ fruit in.
HStack{
Text(fruit.name)
Button("Tap Me"/span>){
secondViewIsPresented.toggle()
SecondView(selectedFruit: fruit)
}
.frame(width:150, height: 35)
.背景(Color.blue)
.buttonStyle(BlueButton())
}
}
}
.sheet(isPresented: $secondViewIsPresented){
//這里我不能訪問所選的水果。
}
}
}
// style to make the button in the row work properly[/span
//我展示它只是為了參考。
結構 BlueButton。ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
配置.標簽
.scaleEffect(configuration.isPressed ? 2 : 1)
}
}
第二種觀點
struct SecondView。View {
var selectedFruit: Fruit[/span
var body: some View {
Text(" Second View: (selectedFruit.name)")
}
}
uj5u.com熱心網友回復:
首先,在按鈕的動作閉合中宣告SecondView沒有任何效果。這只是創建了一個SecondView,然后把它扔掉。
Button("Tap Me"){
secondViewIsPresented.toggle()
SecondView(selectedFruit: fruit) ///nope!
}
你想在一個作業表內呈現所選的水果,對嗎?但正如你所說,問題是你無法從作業表內訪問所選的水果。
這就是sheet(item:onDismiss:content:)的作用。當item不是nil時,這個備用版本的sheet被觸發。然后你就可以在其content閉合中訪問所選的專案。
struct ContentView。View {
// @State private var secondViewIsPresented = false // delete this line
@State private var selectedFruit: Fruit? ///替換成這個?
var fruits = [Fruit(name: "Apple"), Fruit(name: "Orange") ]
var body。some View {
List{
ForEach(fruits){ fruit in.
HStack{
Text(fruit.name)
Button("Tap Me"/span>){
selectedFruit = fruit //> set the fruit。
}
.frame(width:150, height: 35)
.背景(Color.blue)
.buttonStyle(BlueButton())
}
}
}
.sheet(item: $selectedFruit) { fruit in // access selected fruit inside content closure.
SecondView(selectedFruit: fruit)
}
}
}
結果:
uj5u.com熱心網友回復:
我建議你使用EnvironmentObject來做這件事。
水果結構和以前一樣。
水果結構保持不變。
struct Fruit。可識別的{
var id = UUID()
var name:String()
}
你創建了一個ObservableObject,在初始化程序中傳遞給FirstView。
class DataState。ObservableObject {
@Published var fruit: Fruit? = nil?
}
現在,第一個視圖變成了
struct FirstView。View {
@State private var secondViewIsPresented = false
//this has been added
@EnvironmentObject var dataState: DataState var dataState.
var fruits = [
Fruit(name: "Apple") 。
Fruit(name: "Orange")
]
var body。some View {
List{
ForEach(fruits){ fruit in.
HStack{
Text(fruit.name)
Button("Tap Me"/span>){
secondViewIsPresented.toggle()
//這已經被改變了。
dataState.fruit = fruit
SecondView()
}
.frame(width:150, height: 35)
.背景(Color.blue)
.buttonStyle(BlueButton())
}
}
}
.sheet(isPresented: $secondViewIsPresented){
//這里我不能訪問所選的水果。
}
}
}
// style to make the button in the row work properly[/span
//我展示它只是為了參考。
結構 BlueButton。ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
配置.標簽
.scaleEffect(configuration.isPressed ? 2 : 1)
}
}
另外,你現在可以使用EnvironmentObject訪問被傳遞的物件
。struct SecondView。View {
//this has been added@EnvironmentObject var dataState: DataState var dataState.
var body: some View {
Text(" Second View: (dataState.fruit?.name ?""))
}
}
最后,你需要確保在FirstView的初始化程序中傳入DataState
。var appState = DataState()
FirstView()
.environmentObject(self.appState))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307283.html
標籤:
