gpt4 book ai didi

state - “在 View 更新期间修改状态,这将导致未定义的行为。”在文本字段(SwiftUI)上键入时出错

转载 作者:行者123 更新时间:2023-12-04 15:17:15 25 4
gpt4 key购买 nike

我有两个文本字段,分别分配给:

@State  private var emailAddress: String = ""
@State private var password: String = ""

现在,每当我在上面输入内容时,该应用似乎都卡住了,并给了我这个错误:

'Modifying state during view update, this will cause undefined behavior.'



我有一个 StartView():
class UserSettings: ObservableObject {

var didChange = PassthroughSubject<Void, Never>()

@Published var loggedIn : Bool = false {
didSet {
didChange.send(())
}
}
}

struct StartView: View {
@EnvironmentObject var settings: UserSettings

var body: some View {
if settings.loggedIn {
return AnyView(TabbarView())
}else {
return AnyView(ContentView())
}
}

}

我创建了一个具有 loggedIn bool值的UserSettings的ObservableObject类。当用户点击 LogInView()中的“登录”按钮时,此 bool 值将变为 true并出现一个新 View ( TabbarView())

这是LogInView():
struct LogInView: View {
@EnvironmentObject var settings: UserSettings

@State private var emailAddress: String = ""
@State private var password: String = ""

var body: some View {
GeometryReader { geometry in
VStack (alignment: .center){
HStack {
Image("2")
.resizable()
.frame(width: 20, height: 20)
Text("Social App")
.font(.system(size: 12))

}.padding(.top, 30)
.padding(.bottom, 10)

Text("Log In to Your Account")
.font(.title)
.font(.system(size: 14, weight: .bold, design: Font.Design.default))
.padding(.bottom, 50)

TextField("Email", text: self.$emailAddress)
.frame(width: geometry.size.width - 45, height: 50)
.textContentType(.emailAddress)
.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 0))
.accentColor(.red)
.background(Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255))
.cornerRadius(5)


TextField("Password", text: self.$password)
.frame(width: geometry.size.width - 45, height: 50)
.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 0))
.foregroundColor(.gray)
.background(Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255))
.textContentType(.password)
.cornerRadius(5)

Button(action: {
self.settings.loggedIn = true
}) {
HStack {
Text("Log In")
}
.padding()
.frame(width: geometry.size.width - 40, height: 40)
.foregroundColor(Color.white)
.background(Color.blue)
.cornerRadius(5)
}
.padding(.bottom, 40)

Divider()

Button(action: {
print("Take to forget password VC")
}) {
Text("Forgot your password?")
}

Spacer()

}
.padding(.bottom, 90)
}
}
}

我知道如果在修改状态(在文本字段中键入)时更新 View ,则会出现此错误。但是我不会在“登录”屏幕中的任何位置更新 View 。那么为什么会发生此错误。帮助将不胜感激!

最佳答案

这对我有用,您甚至不需要导入Combine!当您使用@Published时,SwiftUI将自动合成objectWillChange主题,并且只要该属性发生突变,就会调用send。如果需要,您仍然可以手动调用.send(),但是在大多数情况下,您不需要这样做。

class UserSettings: ObservableObject {
@Published var loggedIn : Bool = false
}

Beta 5发行说明的摘录:

You can manually conform to ObservableObject by defining an objectWillChange publisher that emits before the object changes. However, by default, ObservableObject automatically synthesizes objectWillChange and emits before any @Published properties change.



这是对我来说很好的完整代码(iPhone Xr和真实设备,iPad 6th Gen):

window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(UserSettings()))

import SwiftUI

struct ContentView: View {
var body: some View {
StartView()
}
}

class UserSettings: ObservableObject {
@Published var loggedIn : Bool = false
}

struct StartView: View {
@EnvironmentObject var settings: UserSettings

var body: some View {
if settings.loggedIn {
return AnyView(Text("LOGGED IN"))
} else {
return AnyView(LogInView())
}
}
}

struct LogInView: View {
@EnvironmentObject var settings: UserSettings


@State private var emailAddress: String = ""
@State private var password: String = ""

var body: some View {
GeometryReader { geometry in
VStack (alignment: .center){
HStack {
Image(systemName: "2.circle.fill")
.resizable()
.frame(width: 20, height: 20)
Text("Social App")
.font(.system(size: 12))

}.padding(.top, 30)
.padding(.bottom, 10)

Text("Log In to Your Account")
.font(.title)
.font(.system(size: 14, weight: .bold, design: Font.Design.default))
.padding(.bottom, 50)

TextField("Email", text: self.$emailAddress)
.frame(width: geometry.size.width - 45, height: 50)
.textContentType(.emailAddress)
.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 0))
.accentColor(.red)
.background(Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255))
.cornerRadius(5)


TextField("Password", text: self.$password)
.frame(width: geometry.size.width - 45, height: 50)
.padding(EdgeInsets(top: 0, leading: 5, bottom: 0, trailing: 0))
.foregroundColor(.gray)
.background(Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255))
.textContentType(.password)
.cornerRadius(5)

Button(action: {
self.settings.loggedIn = true
}) {
HStack {
Text("Log In")
}
.padding()
.frame(width: geometry.size.width - 40, height: 40)
.foregroundColor(Color.white)
.background(Color.blue)
.cornerRadius(5)
}
.padding(.bottom, 40)

Divider()

Button(action: {
print("Take to forget password VC")
}) {
Text("Forgot your password?")
}

Spacer()

}
.padding(.bottom, 90)
}
}
}

关于state - “在 View 更新期间修改状态,这将导致未定义的行为。”在文本字段(SwiftUI)上键入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57340970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com