Push View programmatically in callback, SwiftUI
I've found the answer. If you want to show another view on callback you should
Create state
@State var pushActive = false
When ViewModel notifies that login is successful set
pushActive
totrue
func handleSuccessfullLogin() {
self.pushActive = true
print("handleSuccessfullLogin")
}Create hidden
NavigationLink
and bind to that stateNavigationLink(destination:
ProfileView(viewModel: ProfileViewModelImpl()),
isActive: self.$pushActive) {
EmptyView()
}.hidden()
Programmatically navigate to new view in SwiftUI
You can replace the next view with your login view after a successful login. For example:
struct LoginView: View {
var body: some View {
...
}
}
struct NextView: View {
var body: some View {
...
}
}
// Your starting view
struct ContentView: View {
@EnvironmentObject var userAuth: UserAuth
var body: some View {
if !userAuth.isLoggedin {
LoginView()
} else {
NextView()
}
}
}
You should handle your login process in your data model and use bindings such as @EnvironmentObject
to pass isLoggedin
to your view.
Note: In Xcode Version 11.0 beta 4, to conform to protocol 'BindableObject' the willChange property has to be added
import Combine
class UserAuth: ObservableObject {
let didChange = PassthroughSubject<UserAuth,Never>()
// required to conform to protocol 'ObservableObject'
let willChange = PassthroughSubject<UserAuth,Never>()
func login() {
// login request... on success:
self.isLoggedin = true
}
var isLoggedin = false {
didSet {
didChange.send(self)
}
// willSet {
// willChange.send(self)
// }
}
}
How to switch to another view programmatically in SwiftUI (without a button press)
You can use a NavigationLink
with the isActive
parameter. When you set this parameter to true
it will present a NewView
:
@State var activated: Bool = false
NavigationLink(destination: NewView(), isActive: $activated) {
EmptyView()
}
Then you need to place your NavigationLink
in the NavigationView
to make it work.
How To Switch To A New View On Simple Button Click?
The best way would be to use a NavigationLink
and wrapping the content inside a NavigationView
.
The NavigationView
is used to represent the view hierarchy.
For instance:
// Wrapper for LogIn
struct ContentView: View {
var body: some View {
NavigationView { LogIn() }
}
}
// LogIn
struct LogIn: View {
var body: some View {
VStack {
// Fields for log in
NavigationLink(destination: SignUp()) {
Text("Create New Account")
.foregroundColor(.white)
.font(.title)
.frame(maxWidth: .infinity, alignment: .center)
.background(Color(red: 0.22, green: 0.655, blue: 0.02))
.cornerRadius(8)
.padding(.horizontal, metrics.size.width*0.10)
}
}
}
}
You can find more info in the official docs:
- NavigationView
- NavigationLink
Also [Hacking With Swift] (https://www.hackingwithswift.com/quick-start/swiftui/displaying-a-detail-screen-with-navigationlink) is a great resource for SwiftUI
iOS SwiftUI: pop or dismiss view programmatically
This example uses the new environment var documented in the Beta 5 Release Notes, which was using a value property. It was changed in a later beta to use a wrappedValue property. This example is now current for the GM version. This exact same concept works to dismiss Modal views presented with the .sheet modifier.
import SwiftUI
struct DetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
Button(
"Here is Detail View. Tap to go back.",
action: { self.presentationMode.wrappedValue.dismiss() }
)
}
}
struct RootView: View {
var body: some View {
VStack {
NavigationLink(destination: DetailView())
{ Text("I am Root. Tap for Detail View.") }
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
RootView()
}
}
}
Programmatically navigate to another view controller/scene
I already found the answer
Swift 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
Related Topics
Type Conversion When Using Protocol in Swift
Whats the Swift Animate Withduration Syntax
Break a Number Up to an Array of Individual Digits
Iboutlet and Ibaction in Swift
Multiple Type Constraints in Swift
Why Non Optional Any Can Hold Nil
Perform Assignment Only If Right Side Is Not Nil
Swift Dictionary Default Value
Initialize Class-Instance and Access Variables in Swift
Wkwebview Does Not Load Links to Pdfs
Can You Override Between Extensions in Swift or Not? (Compiler Seems Confused!)
Swiftui Hierarchical Picker With Dynamic Data Crashes
How to Use Indices.Contains() in a Collection Extension in Swift 3
@Noescape Attribute in Swift 1.2
How to Add Alamofire Url Parameters