Reputation: 93
ok so, im on on SwiftUI Xcode 11.3.1 trying to call a second view after login
the working design of the second view is on the file mainView.swift
after the login I'm calling my view like this
Button(action: {
self.status = dologin(username: self.username, password: self.password)
if (self.status){
print ("log in succesfull")
mainMenu() // <-- return of "mainMenu" initializer is unused
}
})
I know you can call a. subview like .sheet(isPresented)
but this won't work for me cause is a login screen.
thanks in advance.
Upvotes: 0
Views: 1606
Reputation: 773
You can also add button in NaigationView like this.
NavigationLink(destination: mainMenu()) {
Button(action: {
}) {
Text("Log in!")
}
}
Upvotes: 0
Reputation: 3275
You need to control views in other way in SwiftUI, you can't just push
or pop
view, like in UIKit. Apple proposes NavigationView
and you can use something like NavigationLink(destination: YourLoginView(), isActive: $someBindingVariable, label: Text(""))
, but I really don't like to play with .navigationBarHidden(true)
and .navigationBarBackButtonHidden(true)
and other staff. There are some options:
.popover(isPresented: $needToLogin) { // login view }
. Either you can use .sheet
. In this case user can move your "Login view" to bottom and use "Main view" as usual:struct MovingViewsSwiftUI: View {
@State private var needToLogin = true
var body: some View {
Rectangle() // your "Main view"
.popover(isPresented: $needToLogin) {
LoginView(needToLogin: self.$needToLogin) // will show you at the end
}
}
}
if...else
statements, for example://...
var body: some View {
if needToLogin {
return AnyView(LoginView)
} else {
return AnyView(MainView)
}
// something else in body
}
ZStack
and control views .opacity
or .offset
. It allows you to achieve interesting animations:struct MovingViewsSwiftUI: View {
@State private var needToLogin = true
var body: some View {
ZStack {
LoginView(needToLogin: $needToLogin)
.opacity(needToLogin ? 1 : 0)
Rectangle()
.opacity(needToLogin ? 0 : 1)
}
}
}
and here is the example of LoginView
:
struct LoginView: View {
@Binding var needToLogin: Bool
@State private var email: String = ""
@State private var password: String = ""
var body: some View {
VStack {
TextField("enter email", text: $email)
TextField("pass", text: $password)
Button(action: {
withAnimation {
self.needToLogin = false
}
}) {
Text("Log in!")
}
}
.padding()
}
}
Upvotes: 2