[英]How to switch to another view programmatically in SwiftUI (without a button press)
I have a problem to switch to another view without pressing a button in my SwiftUI app.我在不按 SwiftUI 应用程序中的按钮的情况下切换到另一个视图时遇到问题。 I can take many examples of what I want to do: 1- Imagine a login form where we need to fill 2 textfields: username and password.
我可以举很多我想做的例子: 1-想象一个登录表单,我们需要填写 2 个文本字段:用户名和密码。 When there's a perfect match we are automatically authenticated.
当完美匹配时,我们会自动进行身份验证。 2- Take my example now.
2-现在以我为例。 I have this view:
我有这样的看法:
What I want to do: when I select a row in the list (ex. Lyon-Perrache TGV) AND if the two textfields are not empty => I'm redirected to another view.我想要做什么:当我 select 在列表中的一行(例如 Lyon-Perrache TGV)并且如果两个文本字段不为空 => 我被重定向到另一个视图。
There's many examples to know how to move to a view from another one, but always with a button click.. (with NavigationLink) I need to do this programatically exactly when the two textfields are filled AND when I select the destination textfield from the list.有很多例子可以知道如何从另一个视图移动到一个视图,但总是单击按钮..(使用 NavigationLink)我需要在两个文本字段被填充时以及当我 select 列表中的目标文本字段时以编程方式执行此操作.
that's a part of my code:这是我的代码的一部分:
import SwiftUI
struct Search: View {
@ObservedObject var webservice = Webservice()
@State var Depart: String = ""
@State var Destination: String = ""
@State var listStation = [Station]()
@State var predictedValues: Array<String> = []
@State var isBeingEditedDeparture: Bool = false
@State var isBeingEditedDestination: Bool = false
@State var hey: Bool = false
@State var activate: Bool = false
init() {
UITableView.appearance().showsVerticalScrollIndicator = false
UITableView.appearance().backgroundColor = .clear
UITableViewCell.appearance().backgroundColor = .clear
//UITableView.appearance().separatorColor = .clear
}
var body: some View {
VStack(alignment: .leading){
PredictingTextField(listStation: self.$listStation , predictedValues: self.$predictedValues,
Depart: self.$Depart, Destination: self.$Destination, isBeingEditedDeparture:
self.$isBeingEditedDeparture, isBeingEditedDestination: self.$isBeingEditedDestination )
.textFieldStyle(RoundedBorderTextFieldStyle())
List() {
ForEach(self.predictedValues, id: \.self){ value in
Text(value)
.onTapGesture {
if(self.isBeingEditedDeparture == true)
{
self.Depart = value
}
else{
self.Destination = value
self.activate = true
}
}
}
}.padding(.top, 10)
}
}
I have declared a variable named "activate" to be the trigger point of moving to the new View.我已经声明了一个名为“activate”的变量作为移动到新视图的触发点。 It's equal to true when we select a row in the list for "destination" textfield.
当我们 select 在“目标”文本字段列表中的一行时,它等于 true。
How can I do to go further?我该怎么做 go 进一步?
Thank you in advance !先感谢您 !
You can use a NavigationLink
with the isActive
parameter.您可以使用带有
isActive
参数的NavigationLink
。 When you set this parameter to true
it will present a NewView
:当您将此参数设置为
true
时,它将显示一个NewView
:
@State var activated: Bool = false
NavigationLink(destination: NewView(), isActive: $activated) {
EmptyView()
}
Then you need to place your NavigationLink
in the NavigationView
to make it work.然后,您需要将
NavigationLink
放在NavigationView
中以使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.