Programmatically Navigate to New View in Swiftui

Push View programmatically in callback, SwiftUI

I've found the answer. If you want to show another view on callback you should

  1. Create state @State var pushActive = false

  2. When ViewModel notifies that login is successful set pushActive to true

    func handleSuccessfullLogin() {
    self.pushActive = true
    print("handleSuccessfullLogin")
    }
  3. Create hidden NavigationLink and bind to that state

    NavigationLink(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



Leave a reply



Submit