Xcode 11 beta 6,iOS Develop beta 8
我认为@Satae属性应该初始化,我不知道SwiftUI的错误还是误解了@State。
操作流程是
代码是这样的。
import SwiftUI
struct ContentView: View {
@State var isShow = false
var body: some View {
Button(action: {self.isShow.toggle()}) {
Text("show another view")
}.sheet(isPresented: $isShow) {
AnotherView()
}
}
}
struct AnotherView: View {
@Environment(\.presentationMode) var presentationMode
@State var text = ""
var body: some View {
VStack {
TextField("Input changing the @State property", text: $text)
Button(action: {self.presentationMode.wrappedValue.dismiss()}) {
Text("Dismiss")
}
}.padding()
}
}
答案 0 :(得分:0)
document.getElementById('tableBody').innerHTML = "<tr><td>"+billSeqId+"<td><a href="
+billImageUrl+" target='_blank'>view</a></td><td>"
+billType+"</td><td>"
+billStatus+"</td><td class='text-right'><div class='form-group'><select class='form-control' onchange='selectChange()' id='sel1'>"
+"<option>Change Status</option><option>Validation Completed</option>"
+"<option>Validation Failed</option>"
+"</select></div></td></tr>";
的寿命尽可能长,因此很可能在解构后的视图之间重用。但是人们可以通过几种方式解决它。
@State
ObservableObject
import SwiftUI
struct InitializeStateView: View {
@State var isShow = false
var body: some View {
Button(action: {self.isShow.toggle()}) {
Text("show another view")
}.sheet(isPresented: $isShow) {
InitAnotherView()
}
}
}
class InitAnotherViewState: ObservableObject {
@Published var wasInitialized = false
}
struct InitAnotherView: View {
init() {
print("Init InitAnotherView")
}
@Environment(\.presentationMode) var presentationMode
@ObservedObject var state = InitAnotherViewState()
@State var text = ""
var body: some View {
print("Redrawing InitAnotherView")
if !state.wasInitialized {
DispatchQueue.main.asyncAfter(deadline: .now()) {
self.text = "hey"
}
state.wasInitialized = true
}
return VStack {
TextField("Input changing the @State property", text: $text)
Button(action: {self.presentationMode.wrappedValue.dismiss()}) {
Text("Dismiss")
}
}.padding()
}
}
而在每个新字符上重画正文ObservableObject