這是基本視圖
struct BaseView<Content: View>: View {
@State private var ShowSFView : Bool = false
let content: Content
init(@ViewBuilder content: () -> Content ) {
self.content = content()
}
//Code for button and URL
}
當我呼叫這個 baseView 時,我需要從另一個 View 將兩個 String 值傳遞給這個 BaseView 。一種用于按鈕標簽,另一種用于 URL。
我無法通過在初始化程式上宣告變數來做到這一點,并出現各種錯誤。我怎樣才能做到這一點?
編輯
baseView 中的初始化器
init(@ViewBuilder content: () -> Content , btnlabel: String) {
self.content = content()
self.btnlabel=btnlabel
}
我如何從另一個視圖呼叫它
BaseView.init(content: () -> _, btnlabel: "")
uj5u.com熱心網友回復:
您可以像普通 init 一樣傳遞任何引數或字串引數。
struct BaseView<Content: View>: View {
@State private var ShowSFView : Bool = false
private let content: Content
let stringOne: String
let stringTwo: String
init(stringOne: String, stringTwo: String, @ViewBuilder content: () -> Content ) {
self.content = content()
self.stringOne = stringOne
self.stringTwo = stringTwo
}
var body: some View {
Text(stringOne)
Text(stringTwo)
}
}
現在你可以像這樣使用它
BaseView(stringOne: "str1", stringTwo: "str2") {
// Content
}
編輯
從您的示例中,您可以像這樣使用它
BaseView(content: {
// Content
}, btnlabel: "Lable")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367361.html
