简体   繁体   English

iOS SwiftUI - NavigationLink 打开新视图并关闭当前视图

[英]iOS SwiftUI - NavigationLink open new view and close the current one

This is the Login() view:这是 Login() 视图:

struct Login: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View{
        
        VStack{
            
            HStack{
                                    
                Button(action: {
                    
                    presentationMode.wrappedValue.dismiss()

                }) {
                    Image(systemName: "xmark")
                        .resizable()
                        .frame(width: 18, height: 18)
                }

            }
            
            
            NavigationLink(destination: CreateAccount().navigationBarBackButtonHidden(true), label: {
                Text("create account")
                
                // and close the current view Login()
            })                
        }       
    }
}

Is it possible to open a new view, in this case CreateAccount() and closing the current view Login()?是否可以打开一个新视图,在这种情况下 CreateAccount() 并关闭当前视图 Login()?

In order to do this, I would suggest skipping the NavigationView altogether, see here for more info.为此,我建议完全跳过NavigationView ,请参阅此处了解更多信息。 An example for your situation:你的情况的一个例子:

//You need an `ObservedObject` to do this, and a overall holder view

enum ViewStates{
//Declare possible views
case ContentView
case Login
case CreateAccount

}

//Then use an observableObject

class viewControl: ObservableObject{
@Published var currentView: ViewStates = .ContentView
}

//Finally, pass this into your views. Take a look at the second part of the tutorial I posted below for more info
//such as using `EnvironmentObject` and adding animation. Example implimentation below:


struct ControllerView: View{
@StateObject var controller: viewControl

var body: some View{
switch controller.currentView{
case .ContentView:
ContentView(controller: controller)
case .Login:
Login(controller: controller)
case .CreateAccount:
CreateAccount(controller: controller)
}
}

}

Next, you need to have @ObservedObject var controller: viewControl in all of your views.接下来,您需要在所有视图中都有@ObservedObject var controller: viewControl Note that you don't need a default statement in the switch clause, because the enum declares all possible values.请注意,您不需要在 switch 子句中使用默认语句,因为enum声明了所有可能的值。 The following is an example CreateAccount view.下面是一个示例 CreateAccount 视图。 You also no longer need the dismiss - in fact, that will no longer work.您也不再需要解雇 - 事实上,这将不再有效。

struct CreateAccount: View{
@ObservedObject var controller: viewControl

var body: some View{
//Content
Button("Dismiss"){
controller.currentView = .ContentView
}
}

}

This will allow you to switch the view by clicking.这将允许您通过单击来切换视图。 Instead of a NavigationLink in ContentView, do this:而不是 ContentView 中的 NavigationLink,请执行以下操作:

Button{
controller.currentView = .CreateAccount
} label: {
Text("Create Account")
}

To go back, you just set the value again.要返回,您只需再次设置该值。 This can also be expanded to show more views.这也可以扩展以显示更多视图。

Second part of the tutorial 教程的第二部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM