根据条件,swiftUI,在按钮单击时移动到 nextView
Move to nextView on button click based on condition, swiftUI
我在这里阅读了很多关于 SwiftUI 中导航的文章,并尝试了一些方法,但没有任何效果。
我正在开发一个手表应用程序,我必须在其中使用两个 TextField。当两个字段都不为空时,我只想在下一个屏幕上移动。这是我的代码。
@State var movetoNextScreen: Int? = nil
NavigationLink(destination: WaterTemprature(), tag: 1,
selection: $movetoNextScreen) {
ZStack {
Text("Next")
.font(.system(size: 12, weight: .semibold))
.frame(alignment: .center)
Button(action: nextBtnPressed) {
if !check_minute_and_seconds_are_empty() {
// move to next screen.
movetoNextScreen = 1
}
}.opacity(0)
}
}
但是当我按下导航 link 时,按钮操作没有调用和控制移动到下一个屏幕。所以我想找到任何可以先检查文本字段然后移动到下一个视图的方法。
尝试相反的方向 - 将按钮放在前面(需要重新设计)并将 link 放在背景中并通过程序激活,例如
Button(action: {
if !check_minute_and_seconds_are_empty() {
movetoNextScreen = 1 // << activate link
}
}) {
Text("Next")
.font(.system(size: 12, weight: .semibold))
.frame(alignment: .center)
}
.background(
NavigationLink(destination: WaterTemprature(), tag: 1,
selection: $movetoNextScreen) { EmptyView() }
)
我在这里阅读了很多关于 SwiftUI 中导航的文章,并尝试了一些方法,但没有任何效果。
我正在开发一个手表应用程序,我必须在其中使用两个 TextField。当两个字段都不为空时,我只想在下一个屏幕上移动。这是我的代码。
@State var movetoNextScreen: Int? = nil
NavigationLink(destination: WaterTemprature(), tag: 1,
selection: $movetoNextScreen) {
ZStack {
Text("Next")
.font(.system(size: 12, weight: .semibold))
.frame(alignment: .center)
Button(action: nextBtnPressed) {
if !check_minute_and_seconds_are_empty() {
// move to next screen.
movetoNextScreen = 1
}
}.opacity(0)
}
}
但是当我按下导航 link 时,按钮操作没有调用和控制移动到下一个屏幕。所以我想找到任何可以先检查文本字段然后移动到下一个视图的方法。
尝试相反的方向 - 将按钮放在前面(需要重新设计)并将 link 放在背景中并通过程序激活,例如
Button(action: {
if !check_minute_and_seconds_are_empty() {
movetoNextScreen = 1 // << activate link
}
}) {
Text("Next")
.font(.system(size: 12, weight: .semibold))
.frame(alignment: .center)
}
.background(
NavigationLink(destination: WaterTemprature(), tag: 1,
selection: $movetoNextScreen) { EmptyView() }
)