0

In a SwiftUI macOS app that uses the SwiftUI App lifecycle I have been struggling to understand how to launch a new window and then assign it some initial state.

Using Xcode 12.5’s new project we get:

import SwiftUI

@main
struct StackOverflowExampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

To keep the question simple, let’s say that I would like to launch a new window every minute and send the time the window was launched to the view. For this we can use a Timer like so:

Timer.scheduledTimer(
    withTimeInterval: 60,
    repeats: true,
    block: {_ in
        // do something here that launches a new window containing
        // ContentView and sends it the current time
    }
)

Any ideas on how to proceed?

casr
  • 1,166
  • 2
  • 11
  • 17
  • 2
    Why would you do that in first place? It would make no sense, instead update your View aka ContentView! Even if you could launch a new WindowGroup it would be very expensive in cpu power, when a WindowGroup get created it makes lots of code runs underhood of SwiftUI, you do not wana do that. – ios coder Jul 27 '21 at 17:03
  • 1
    I don't think I want to launch a new WindowGroup as, if I understand it correctly, a WindowGroup holds a collection of windows. I would like to launch a new window inside the group and pass it some data arbitrarily. In other words, it won't be responding to something a file open but another type of event occurring in the code somewhere. In the example above the 'event' is the Timer call back block but it could be anything. – casr Jul 27 '21 at 18:05
  • 2
    This may have solutions you're looking for: https://stackoverflow.com/questions/62915324/swiftui-2-the-way-to-open-view-in-new-window It's clunky, but SwiftUI is pretty limited for window management, unfortunately. – jnpdx Jul 27 '21 at 18:07

0 Answers0