如何在iOS上的SwiftUI中导航到电子邮件验证后的新视图?

fd3cxomn  于 2023-05-30  发布在  iOS
关注(0)|答案(2)|浏览(228)

我目前正在为登录和注册我的IOS应用程序的用户路径作为一个初学者,我需要有我的代码,我如何当电子邮件地址是有效的,并在我目前的项目中更改视图的意见,我已经创建了不同的屏幕几个视图
先谢谢你了

import SwiftUI

struct Registration: View {
    // MARK: - Properties
    @State private var email = ""
    @State private var Error = false
    @State private var ValidationButton = false
    
    // Check email
    var isEmailValid: Bool {
        let emailRegex = #"^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"#
        return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: email)
    }
    
    // MARK: - Body
    var body: some View {
        NavigationView {
            VStack(alignment: .leading, spacing: 40) {
                VStack(spacing: 18) {
                    VStack(alignment: .leading, spacing: 5) {
                        Text("Welcome!")
                            .font(.title)
                            .fontWeight(.bold)
                        Text("Enter your e-mail in order to log in or create an account")
                            .multilineTextAlignment(.leading)
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    
                    HStack(spacing: 16) {
                        Image(systemName: "envelope.fill")
                            .imageScale(.large)
                            .foregroundColor(Color.primary)
                        
                        TextField("Email", text: $email, onEditingChanged: { editing in
                            if !editing {
                                Error = !isEmailValid
                                ValidationButton = isEmailValid
                            }
                        })
                        .font(.system(size: 20))
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .foregroundColor(.primary)
                        
                        if ValidationButton {
                            Button(action: {
                                print("Email validated")
                            }) {
                                Image(systemName: "arrow.forward.square.fill")
                                    .font(.system(size: 35))
                            }
                            .padding(.leading, 8)
                        }
                    }
                    .padding(.horizontal, 16)
                    .frame(minHeight: 55)
                    .overlay(
                        RoundedRectangle(cornerRadius: 6)
                            .stroke(Error ? Color.red : Color.primary)
                    )
                    
                    if Error {
                        Text("Please enter a valid email address")
                            .foregroundColor(.red)
                            .padding(.leading, 16)
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                }
                .foregroundColor(.primary)
                
                HStack(spacing: 10) {
                    Rectangle()
                        .fill(Color(.separator))
                        .frame(height: 1)
                    Text("or continue with")
                        .fixedSize(horizontal: true, vertical: true)
                        .frame(maxWidth: .infinity)
                    Rectangle()
                        .fill(Color(.separator))
                        .frame(height: 1)
                }
                
                HStack(alignment: .top, spacing: 12) {
                    Button(action: {
                        print("Tap on Apple button")
                    }) {
                        ZStack {
                            RoundedRectangle(cornerRadius: 6)
                                .fill(Color(.systemGray5))
                            
                            Image(systemName: "apple.logo")
                                .font(.system(size: 19))
                                .foregroundColor(Color.primary)
                        }
                    }
                    
                    Button(action: {
                        print("Tap on Google button")
                    }) {
                        ZStack {
                            RoundedRectangle(cornerRadius: 6)
                                .fill(Color(.systemGray5))
                            
                            Image(systemName: "apple.logo")
                                .font(.system(size: 19))
                                .foregroundColor(Color.primary)
                        }
                    }
                    
                    Button(action: {
                        print("Tap on Facebook button")
                    }) {
                        ZStack {
                            RoundedRectangle(cornerRadius: 6)
                                .fill(Color(.systemGray5))
                            
                            Image(systemName: "apple.logo")
                                .font(.system(size: 19))
                                .foregroundColor(Color.primary)
                        }
                    }
                }
                .frame(maxHeight: 50)
                
            }
            .padding(.horizontal, 16)
            .background(Color(.systemBackground))
            .navigationBarHidden(true)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct RegistrationView_Previews: PreviewProvider {
    static var previews: some View {
        Registration()
    }
}

为了解决我的问题,我试图通过在苹果官方文档上查找一个NavigationLink,但它不起作用,我不知道这是否是好方法,谢谢你

dsekswqp

dsekswqp1#

如果你想利用NavigationLink,你可以做这样的事情,而不是按钮:

if ValidationButton {
     NavigationLink {
          Text("hello world")
     } label: {
          Image(systemName: "arrow.forward.square.fill")
               .font(.system(size: 35))
     }
     .padding(.leading, 8)
}
pu82cl6c

pu82cl6c2#

如果要在验证电子邮件后导航到下一个视图,可以使用NavigationLink:

@State private var goToHomeView = false // declare this variable

if ValidationButton {
                        Button(action: {
                            print("Email validated")
                            goToHomeView = true
                        }) {
                            Image(systemName: "arrow.forward.square.fill")
                                .font(.system(size: 35))
                        }
                        .padding(.leading, 8)
                        .background(
                            NavigationLink(
                                destination: HomeView(),
                                isActive: $goToHomeView,
                                label: {
                                    EmptyView()
                                })
                            .hidden()
                        )
                    }

相关问题