[英]SwiftUI navigate to a new view by pressing Button
I am trying to navigate to a new SwiftUI file that I called HomePageView (Currently just consist of a red background and a text that says Home page.) The code below I tried to integrate with my Button which is 1 of 3 buttons on my initial view which is the ContentView.我正在尝试导航到一个名为 HomePageView 的新 SwiftUI 文件(目前只包含一个红色背景和一个显示主页的文本。)下面的代码我试图与我的 Button 集成,这是我初始时的 3 个按钮之一视图即 ContentView。 There are no errors but when I run my code my Login button, it shows the "Login Tapped!"
没有错误,但是当我运行我的代码时,我的登录按钮显示“登录已点击!” text, but does not take me to the HomePageView.
文本,但不会带我到 HomePageView。 Am I using NavigationLink incorrectly?
我是否错误地使用了 NavigationLink? I know the next problem I will run into is with multiple buttons on one page leading to different destinations, any easy way to solve this, I am trying the tag method?
我知道我将遇到的下一个问题是一个页面上的多个按钮通向不同的目的地,有什么简单的方法可以解决这个问题,我正在尝试标记方法?
Note: There is other code in the some View text that are just images and textfields, as well as the two other buttons注意:一些视图文本中还有其他代码只是图像和文本字段,以及另外两个按钮
@State private var current: Int? = nil
var body: some View {
NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
EmptyView()
}
Button(action: {
self.current = 1
print("Login tapped!")
}) {
Text("Login")
.fontWeight(.bold)
.foregroundColor(.orange)
.frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
.cornerRadius(50)
.overlay(
Capsule(style: .continuous)
.stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
.frame(width: deviceSize.size.width, alignment: .center)
}.offset(y: deviceSize.size.height*(560/812))
}
correct me if my thinking as code below is also about your idea.如果我的想法如下代码也与您的想法有关,请纠正我。
var body: some View {
NavigationView {
NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
Button(action: {
print("AAAA")
}) {
Text("Login")
.fontWeight(.bold)
.foregroundColor(.orange)
.frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
.cornerRadius(50)
.overlay(
Capsule(style: .continuous)
.stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
.frame(width: deviceSize.size.width, alignment: .center)
}
}
}
}
If like as above, that correct what happening to you because it just recognized action of button.如果像上面那样,那更正发生在你身上的事情,因为它只是识别按钮的动作。 Resolve this just remove button, only put text in NavigationLink as below:
解决这个只是删除按钮,只在 NavigationLink 中放置文本,如下所示:
var body: some View {
NavigationView {
NavigationLink(destination: HomePageView(), tag: 1, selection: self.$current) {
Text("Login")
.fontWeight(.bold)
.foregroundColor(.orange)
.frame(width: deviceSize.size.width*(275/375), height: deviceSize.size.height*(45/812))
.cornerRadius(50)
.overlay(
Capsule(style: .continuous)
.stroke(Color.orange, style: StrokeStyle(lineWidth: 2)))
.frame(width: deviceSize.size.width, alignment: .center)
}
}
}
In my project I used another solution.在我的项目中,我使用了另一种解决方案。 I don't know if this maybe works for you too:
我不知道这是否也适合你:
A created a mother view with A 创建了一个母视图
if current == 0 {
CurrentView()
} else {
HomePageView()
}
You can also animate it with withAnimation()
(see https://developer.apple.com/documentation/swiftui/3279151-withanimation )您还可以使用
withAnimation()
设置动画(请参阅https://developer.apple.com/documentation/swiftui/3279151-withanimation )
If you want to take a look at my implementation you can see it here: https://github.com/tristanratz/ChatApp/blob/master/ClientApp/MainView.swift如果你想看看我的实现,你可以在这里看到它: https : //github.com/tristanratz/ChatApp/blob/master/ClientApp/MainView.swift
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.