这是我的表单{}结构
Form {
Section(header: Text("Settings")) {
Toggle(isOn: $allowEmailNotifications) {
Text("Ok to Email Tee Times, etc.")
.font(stateManager.myNormalHNFont)
}
.padding()
.onChange(of: allowEmailNotifications) { newValue in
me.okToSendEmail = newValue
}
}
Section(header: Text("Utilities")) {
NavigationLink(
destination: PlayerMigrationView(),
tag: "ClaimMyRounds",
selection: $selection
) {
Button {
self.selection = "ClaimMyRounds"
} label: {
Text("Consolidate My Rounds To This Account")
.font(stateManager.myNormalHNFont)
}
.padding()
.buttonStyle(BorderlessButtonStyle())
}
}
}
我已阅读文档并一直尝试让新格式正常工作但无济于事。当我尝试这种格式时,我在 Form { 行上收到一个错误,表示我正在将一个闭包传递给不接受一个的视图...
Form {
Section(header: Text("Settings")) {
Toggle(isOn: $allowEmailNotifications) {
Text("Ok to Email Tee Times, etc.")
.font(stateManager.myNormalHNFont)
}
.padding()
.onChange(of: allowEmailNotifications) { newValue in
me.okToSendEmail = newValue
}
}
Section(header: Text("Utilities")) {
Button {
self.selection = "ClaimMyRounds"
} label: {
Text("Consolidate My Rounds To This Account")
.font(stateManager.myNormalHNFont)
}
.padding()
.buttonStyle(BorderlessButtonStyle())
.background (
NavigationLink(value: "ClaimMyRounds", selection: $selection) { EmptyView() }
.navigationDestination(for: String.self) { _ in PlayerMigrationView() }
)
}
}
弃用警告可能是由您尝试使用的
NavigationLink
初始化程序引起的。 init(destination:tag:selection:label:)
的文档建议使用 init(value:label:)
作为替代方案。
您还可以简化使用
NavigationLink
的方式,因为链接的行为就像按钮一样。因此不需要用链接包裹按钮,或者用按钮包裹链接。只需使用 NavigationLink
,根本不涉及 Button
。
然后正如 lorem ipsum 评论的那样,您需要使用
NavigationStack
而不是 NavigationView
。
所以尝试将其更改为这样的内容:
NavigationStack {
Form {
Section(header: Text("Settings")) {
Toggle(isOn: $allowEmailNotifications) {
Text("Ok to Email Tee Times, etc.")
.font(stateManager.myNormalHNFont)
}
.padding()
.onChange(of: allowEmailNotifications) { newValue in
me.okToSendEmail = newValue
}
}
Section(header: Text("Utilities")) {
NavigationLink(value: "ClaimMyRounds") {
Text("Consolidate My Rounds To This Account")
.font(stateManager.myNormalHNFont)
}
.padding()
}
}
.navigationDestination(for: String.self) { selection in
PlayerMigrationView()
}
}
如果您针对 iOS 17 进行编译,那么您还会发现您的
onChange
回调也已被弃用。如果你想修复它,只需将其更改为:
.onChange(of: allowEmailNotifications) { oldValue, newValue in