Skip to content

Instantly share code, notes, and snippets.

@gahntpo
gahntpo / NavigationWithEnvironment.swift
Created June 21, 2020 11:33
SwitUI problem with NavigationLinks and environmentObjects
NavigationLink(destination: FirstDetailView().environmentObject(self.nav),
isActive: self.$nav.detailIsShown) {
Text("show me the details")
}
@gahntpo
gahntpo / MainViewWithOnboarding.swift
Last active June 21, 2020 10:11
Show onboarding during first launch
import SwiftUI
struct MainView: View {
@EnvironmentObject var nav: NavigationController
var body: some View {
ZStack {
if nav.hasSeenOnboarding {
MainContentView()
}else {
OnboardingView()
@gahntpo
gahntpo / NavigationOnboardingController.swift
Last active June 21, 2020 09:54
storing information of the onboarding in the NavigationController
import Foundation
import Combine
class NavigationController: ObservableObject {
@Published var hasSeenOnboarding: Bool
var subscriptions = Set<AnyCancellable>()
init() {
//initilizing by looking up the value from UserDefaults
@gahntpo
gahntpo / NavigationAuthController.swift
Last active June 21, 2020 08:39
changing the ui when the use logs in/out
import Foundation
import Combine
class NavigationAuthController: ObservableObject {
@Published var selection: Int = 1
@Published var detailIsShown: Bool = false
@Published var alertIsShown: Bool = false
var subscriptions = Set<AnyCancellable>()
@gahntpo
gahntpo / CollapsableView.swift
Last active June 21, 2020 08:32
collapsing navigation stacks in swiftUI
import SwiftUI
struct FirstDetailView: View {
@EnvironmentObject var nav: NavigationController
var body: some View {
VStack {
Text("Whaterever")
Button(action: {
self.nav.detailIsShown = false
}) {
@gahntpo
gahntpo / FirstDetailView.swift
Last active June 21, 2020 08:11
using NavigationController to switch between tabs
import SwiftUI
struct FirstDetailView: View {
@EnvironmentObject var nav: NavigationController
var body: some View {
VStack {
Text("show me the details")
Button(action: {
self.nav.selection = 1
}) {
@gahntpo
gahntpo / MainContentView.swift
Last active June 21, 2020 08:09
ContentView with NavigationController in the environment
import SwiftUI
struct ContentView: View {
@EnvironmentObject var nav: NavigationController
var body: some View {
// binding TabView selection to NavigationController
TabView(selection: self.$nav.selection){
FirstView()
@gahntpo
gahntpo / SceneDelegte.swift
Last active June 21, 2020 07:45
Placing the NavigationController in the environment
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//add the NavigationController like so:
@gahntpo
gahntpo / NavigationController.swift
Created June 21, 2020 07:36
using a class to hold all navigation state variables
import Foundation
class NavigationController: ObservableObject {
@Published var selection: Int = 1
@Published var detailIsShown: Bool = false
@Published var alertIsShown: Bool = false
}
@gahntpo
gahntpo / AlertExampleView.swift
Last active June 21, 2020 07:26
using alerts in swift
struct AlertExampleView: View {
@State var showAlert: Bool = false
var body: some View {
VStack {
Text("This is an example").font(.headline)
Button(action: {
self.showAlert = true
}) {
Text("show alert sheet")
}