我知道有PresentationButton
和NavigationButton
,以便在最新的SwiftUI
中更改视图。但是,我想做一个简单的操作。当用户单击签名按钮时,如果凭据正确,它将签名,但也可以进行签名(在这种情况下,请更改视图(。但是,我无法检查它们在PresentationButton
中是否正确,并且无法以普通按钮更改视图。
有其他方法可以做吗?
@IBAction func signInClicked(_ sender: Any) {
if emailText.text != "" && passwordText.text != "" {
Auth.auth().signIn(withEmail: emailText.text!, password: passwordText.text!) { (userdata, error) in
if error != nil {
//error
} else {
performSegue(withIdentifier: "toFeedActivity", sender: nil)
}
}
} else {
//error
}
}
这是一种方式。
struct AppContentView: View {
@State var signInSuccess = false
var body: some View {
return Group {
if signInSuccess {
AppHome()
}
else {
LoginFormView(signInSuccess: $signInSuccess)
}
}
}
}
struct LoginFormView : View {
@State private var userName: String = ""
@State private var password: String = ""
@State private var showError = false
@Binding var signInSuccess: Bool
var body: some View {
VStack {
HStack {
Text("User name")
TextField("type here", text: $userName)
}.padding()
HStack {
Text(" Password")
TextField("type here", text: $password)
.textContentType(.password)
}.padding()
Button(action: {
// Your auth logic
if(self.userName == self.password) {
self.signInSuccess = true
}
else {
self.showError = true
}
}) {
Text("Sign in")
}
if showError {
Text("Incorrect username/password").foregroundColor(Color.red)
}
}
}
}
struct AppHome: View {
var body: some View {
VStack {
Text("Hello freaky world!")
Text("You are signed in.")
}
}
}
我在一个应用程序中也有同样的需求,我找到了一个解决方案...
基本上,您需要在导航视图中插入主视图,然后在视图中添加一个隐形navigationLink,创建一个@State var,该@State var在您想推动视图时控制并在登录回调上更改其值...
那是代码:
struct ContentView: View {
@State var showView = false
var body: some View {
NavigationView {
VStack {
Button(action: {
print("*** Login in progress... ***")
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.showView = true
}
}) {
Text("Push me and go on")
}
//MARK: - NAVIGATION LINKS
NavigationLink(destination: PushedView(), isActive: $showView) {
EmptyView()
}
}
}
}
}
struct PushedView: View {
var body: some View {
Text("This is your pushed view...")
.font(.largeTitle)
.fontWeight(.heavy)
}
}
尝试使用状态&.Sheet
struct ContentView: View {
@State var showingDetail = false
var body: some View {
Button(action: {
self.showingDetail.toggle()
}) {
Text("Show Detail")
}.sheet(isPresented: $showingDetail) {
DetailView()
}
}
}
您可以使用标签使用导航链接,
这是代码:
首先,声明标签var
@State var tag : Int? = nil
然后创建您的按钮视图:
Button("Log In", action: {
Auth.auth().signIn(withEmail: self.email, password: self.password, completion: { (user, error) in
if error == nil {
self.tag = 1
print("success")
}else{
print(error!.localizedDescription)
}
})
因此,当登录成功标签将成为1时,标签将成为1时,您的导航链接将被执行
导航链接代码:
NavigationLink(destination: HomeView(), tag: 1, selection: $tag) {
EmptyView()
}.disabled(true)
如果您使用的是form uses use .Disabled,因为这里将在表单上看到空视图,并且您不希望用户单击它并转到Homeview。