UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How do you dismiss a sheet and launch a different sheet from ContentView?

Forums > SwiftUI

Hello,

For my app, I have a welcome screen that intro's what the app does and allows the user to create their first item. When the user clicks the button I'd like to dismiss the 'welcomeScreen' sheet and and then launch the 'newRemindr' sheet.

I tried to achieve this by creating an observable object with an 'addNewTrigger' boolean set to false. When I click the Add New Reminder button on the welcomeScreen, the button's action causes the welcomeScreen to dismiss and toggles the 'addNewTrigger' boolean to True. (I've verified this is working with Print Statements). However content view is listening to that same observed object to launch the 'newRemindr' sheet but that action doesn't seem to be working.

Can somebody please take a look at the code and see where I am going wrong? Or suggest an alternative that can provide the type of functionality.

I really appreciate all the help. Thanks!

Code Below:

welcomeScreen

import SwiftUI
import Combine

struct welcomeScreen: View {
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>

    @ObservedObject var addNewReminder = showAddScreen()

    var body: some View {
        NavigationView {
            ZStack (alignment: .center) {

                LinearGradient(gradient: Gradient(colors: [Color.white, Color.white, Color.gray]), startPoint: .top, endPoint: .bottom)
                    .edgesIgnoringSafeArea(.all)

                Image("Ellipse2")
                    .offset(y: -475)

                VStack {
                    Spacer()
                    Text("Welcome to")
                        .foregroundColor(.white)
                        .fontWeight(.bold)
                    Image("RemindrLogoWhite")
                    Spacer()
                    Text("What is remindr?")
                        .font(.title)
                        .fontWeight(.bold)
                        .padding(.bottom, 25)
                    Text("Remindr is a simple app designed to help you schedule random reminders with the goal of clearing your mind.\n\nRemind yourself to check in with your body, set up positive affirmations, set your intentions; Whatever it is, the power is up to you.")
                        .padding(.horizontal, 25)
                        .padding(.bottom, 25)
                    Text("Click below to get started:")
                        .fontWeight(.bold)

                    // Add New Reminder Button
                    Button(action: {
                        self.mode.wrappedValue.dismiss()
                        print("Add Reminder Button from Welcome Screen is Tapped")
                        self.addNewReminder.addNewTrigger.toggle()
                        print("var addNewTrigger has been changed to \(self.addNewReminder.addNewTrigger)")
                    }) {
                        Image("addButton")
                            .renderingMode(.original)
                    }.padding(.bottom, 25)

                    Spacer()

                } .frame(maxWidth: UIScreen.main.bounds.width,
                         maxHeight: UIScreen.main.bounds.height)
            }
            .navigationBarTitle(Text(""), displayMode: .automatic)
            .navigationBarItems(trailing: Button(action: {
                self.mode.wrappedValue.dismiss()

            }, label: {
                Image(systemName: "xmark")
                    .foregroundColor(.white)
            }))
        }

    }
}

ContentView:

import SwiftUI
import CoreData

class showAddScreen: ObservableObject {
    @Published var addNewTrigger = false
}

struct ContentView: View {

    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: ReminderEntity.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \ReminderEntity.dateCreated, ascending: false)])
    var reminder: FetchedResults<ReminderEntity>

    // Sheet Control
    @ObservedObject var addNewReminder = showAddScreen()
    //@State private var showingAddScreen = false
    @State var showWelcomeScreen = false

    let emojiList = EmojiList()

    //Toggle Control
    @State var notifyOn = true

    // Save Items Function
    func saveItems() {
        do {
            try moc.save()
        } catch {
            print(error)
        }
    }

    // Delete Item Function
    func deleteItem(indexSet: IndexSet) {
        let source = indexSet.first!
        let listItem = reminder[source]
        moc.delete(listItem)
    }

    // View Controller
    var body: some View {
        VStack {
            NavigationView {
                ZStack (alignment: .top) {

                    // List View
                    List {
                        ForEach(reminder, id: \.self) { notification in
                            NavigationLink(destination: editRemindr(reminder: notification,
                                                                    notifyOn: notification.notifyOn,
                                                                    emojiChoice: Int(notification.emojiChoice),
                                                                    notification: notification.notification ?? "unknown",
                                                                    notes: notification.notes ?? "unknown")) {
                                                                        // Text within List View
                                                                        HStack {
                                                                            // MARK: TODO
                                                                            //Toggle("NotifyOn", isOn: self.$notifyOn)
                                                                            //    .labelsHidden() // Hides the label/title
                                                                            Text("\(self.emojiList.emojis[Int(notification.emojiChoice)]) \(notification.notification!)")
                                                                        }
                            }
                        } 
                        .onDelete(perform: deleteItem)
                    }.lineLimit(1)

                        // Navigation Items
                        .navigationBarTitle("", displayMode: .inline)
                        .navigationBarItems(
                            leading:
                            HStack {

                                Button(action: {
                                    self.showWelcomeScreen.toggle()
                                }) {
                                    Image(systemName: "info.circle.fill")
                                        .font(.system(size: 24, weight: .regular))
                                }.foregroundColor(.gray)

                                // Positioning Remindr Logo on Navigation
                                Image("remindrLogoSmall")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    //.frame(width: 60, height: 60, alignment: .center)
                                    .padding(.leading, 83)
                                    .padding(.top, -10)
                            },

                            // Global Settings Navigation Item
                            trailing: NavigationLink(destination: globalSettings()){
                                Image("settings")
                                    .font(Font.title.weight(.ultraLight))
                            }.foregroundColor(.gray)
                    )

                    // Add New Reminder Button
                    VStack {
                        Spacer()
                        Button(action: { self.addNewReminder.addNewTrigger.toggle()
                        }) {
                            Image("addButton")
                                .renderingMode(.original)
                        }
                        .sheet(isPresented: $addNewReminder.addNewTrigger) {

                            newRemindr().environment(\.managedObjectContext, self.moc)

                        }
                    }
                }
            }  .sheet(isPresented: $showWelcomeScreen) {
                welcomeScreen()
            }
        }
    }
}

Functionally, this is what I am trying to achieve (except I had to click twice to do so...)

3      

Control sheets representation with Identifiable items ...

3      

Solved!

struct welcomeScreen: View {
@Environment(\.presentationMode) var mode: Binding<PresentationMode>

@ObservedObject var addNewReminder: showAddScreen // << declare to be injected

// ... other code

and in ContentView

}  .sheet(isPresented: $showWelcomeScreen) {
    welcomeScreen(addNewReminder: self.addNewReminder)    // << inject !!
}

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.