TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Displaying one sheet after another - SwiftUI

Forums > SwiftUI

I have one button which triggers a new sheet to be displayed. Within that new sheet I have other buttons that also bring up a new sheet. Once this second one has been used I want to dismiss both open sheets and go back to the initial view.

Any ideas on how I do this?

3      

Let’s see how this works with a simple example.

Imagine you have an invoice template you use on a regular basis, but it’s a pain to clear out all the values each time you need to start over. Well, you can add a button to Google Sheets so you can run scripts and clear your invoice with a single button click.

The user can enter information into cells B5, B8, E5 and E6 (shown in yellow).

In the script editor, accessed through Tools > Script Editor, add a very simple script to clear these specific cells out:

function clearInvoice() { var sheet = SpreadsheetApp.getActiveSheet(); var invoiceNumber = sheet.getRange("B5").clearContent(); var invoiceAmount = sheet.getRange("B8").clearContent(); var invoiceTo = sheet.getRange("E5").clearContent(); var invoiceFrom = sheet.getRange("E6").clearContent(); } You can run this function from the script editor and it will clear out the contents of the invoice.

But that’s a pain.

You don’t want to have to open up the script editor every time. You want to do that directly from your Google Sheet.

To do that, add a Google Sheets button. Homepage

You add a button via the Insert > Drawing menu.

This brings up the drawing editor where you can easily add a box and style it to look like a button:

Note: to edit or move the button after you’ve assigned it to a script, you now need to right-click on it.

3      

It seems HWS forums are being "hacked" or intruded by some weird bots or something. But anyway!

I am not sure about displaying modal views on top of modal views to be honest, but since the sheet will present a view, you can use your buttons to toggle a @State property that updates the UI with different elements.

Let's say the view only has one Text view in it by default. Upon button press, you toggle your @State property, which then updates the UI, and your body now shows 2 TextFields and a save button (which dismisses the modal view).

So the body of the view being shown in the sheet needs to display it's UI from within an if ... else statement.

if isDefaultScreen {
  //show something
} else {
  //show something else
}

Hope that helps

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.