英文:
Entering new view after button press (Swift)
问题
struct EnterOptions: View {
var body: some View {
VStack{
Button(action: {
// 在这里添加操作
} ){
Text("SIGN UP").frame(maxWidth: 300)
.frame(maxHeight: .greatestFiniteMagnitude)
.font(.largeTitle)
.fontWeight(.heavy)
}
}
}
}
struct makeM: View {
@StateObject var viewModel: makeM.ViewModel = .init()
@StateObject var home: makeM.LoggedIn = .init()
var body: some View {
VStack{
TextField("Username", text: $viewModel.username)
TextField("Password", text: $viewModel.password)
TextField("Email", text: $viewModel.email)
Button("Sign Up", action: {
Task{await viewModel.signUp() }
})
Button("Sign In", action: {
Task{ await viewModel.signIn() }
})
//if(viewModel.si == true){
Button("Sign Out", action: {
Task{ await viewModel.signOutLocally() }
})
//}
}
.padding(.horizontal, 20)
}
}
// 点击按钮后,我希望用户看到这个屏幕。我尝试使用"makeM()"或将MakeM()放在WindowGroup()中,但不确定如何使用正确的语法切换屏幕供用户使用。在第一个代码片段的"//Action Here"部分遇到问题。我是不是走错了路?不想使用Storyboard
英文:
struct EnterOptions: View {
var body: some View {
VStack{
Button(action: {
//Action here
} ){
Text("SIGN UP").frame(maxWidth: 300)
.frame(maxHeight: .greatestFiniteMagnitude)
.font(.largeTitle)
.fontWeight(.heavy)
}
I create a button where I want the user to decide to sign up or login. Just the sign up is shown.
struct makeM: View {
@StateObject var viewModel: makeM.ViewModel = .init()
@StateObject var home: makeM.LoggedIn = .init()
var body: some View {
VStack{
TextField("Username", text: $viewModel.username)
TextField("Password", text: $viewModel.password)
TextField("Email", text: $viewModel.email)
Button("Sign Up", action: {
Task{await viewModel.signUp() }} )
Button("Sign In", action: {
Task{ await viewModel.signIn() }
})
//if(viewModel.si == true){
Button("Sign Out", action: {
Task{ await viewModel.signOutLocally() }
})
//}
}
.padding(.horizontal, 20)
}
I want the user to see this screen after clicking the button. I've tried using "makeM()" or putting MakeM() in a WindowGroup() but not sure how I can switch screens for the user using the right syntax. Having trouble in the "//Action Here" part in the first code snippet. Am I going about this the wrong way? Not trying to use storyboard
答案1
得分: 1
你应该使用 NavigationLink 如果你想要显示另一个屏幕,但确保整个视图被 NavigationStack 或 NavigationView 包装。
例如:
struct EnterOptions: View {
var body: some View {
NavigationStack {
VStack{
NavigationLink {
// 目标
} label: {
Text("注册").frame(maxWidth: 300)
.frame(maxHeight: .greatestFiniteMagnitude)
.font(.largeTitle)
.fontWeight(.heavy)
}
}
}
}
}
英文:
You should use NavigationLink if you want to show another screen, but make sure that whole view is wrapped by NavigationStack or NavigationView.
For example:
struct EnterOptions: View {
var body: some View {
NavigationStack {
VStack{
NavigationLink {
// Destination
} label: {
Text("SIGN UP").frame(maxWidth: 300)
.frame(maxHeight: .greatestFiniteMagnitude)
.font(.largeTitle)
.fontWeight(.heavy)
}
}
答案2
得分: 0
如果您希望自定义视图之间的过渡动画,您可能需要使用ZStack来呈现第二个视图。此帖子 - https://stackoverflow.com/questions/75512163/how-to-change-views-without-navigation 包含了一个解释如何在不使用导航视图的情况下更改视图的答案。
英文:
if you prefer a custom animation for the transition between the views you might have to use a ZStack to present the second view. This post - https://stackoverflow.com/questions/75512163/how-to-change-views-without-navigation contains an answer that explains the different ways to change views without using the navigation view.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论