Environment (Simulator & Device):
- iOS 17.5.1
- iOS 16.7
- iOS 18.0 beta 3
Steps to Reproduce:
- Build and run
- On the main screen, tap “First”.
- Then tap “Second”.
Observed Behavior:
- On iOS 17.5.1, the StateObject attached to DetailView is initialized twice. The expected behavior is that the StateObject should be initialized only once.
- On iOS 16.7, the StateObject is initialized only once, as expected.
- On iOS 18.0 beta 3, the StateObject is initialized only once initially. However, if you swipe down to dismiss the DetailView, the StateObject is unexpectedly initialized again.
Expected Behavior:
- The StateObject should be initialized only once, and should not be reinitialized when dismissing the DetailView.
Please investigate this issue. FB14283951
import SwiftUI class Object: ObservableObject { @Published var flag = false init() { print("object init") } } struct DetailView: View { @StateObject var object = Object() var body: some View { Text("\(object.flag)") } } struct ContentView: View { @State var showingFirst = false @State var showingSecond = false var body: some View { NavigationStack { ZStack { Button("First") { showingFirst = true } .sheet(isPresented: $showingFirst) { NavigationStack { // replace this ZStack with Form, List, issue exists. // remove this ZStack, issue gone. ZStack { Button("Second") { showingSecond = true } .sheet(isPresented: $showingSecond) { DetailView() } } } // 2nd NavigationStack } // 1st sheet } // ZStack } // 1st NavigationStack } // body } // ContentView