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
Post
Replies
Boosts
Views
Activity
Xcode 13 Beta 5 stuck on "Making iPhone ready for development" with iOS 15 Beta 5 iPhone, won't start installing & debugging code on the device. Waited for about 1 hour, still same.
Tried restoring the iPhone with 15B5 IPSW, or restoring to 14.7.1 then updating to 15B5, both did not work.
Anybody has this same issue?