SwiftUI bug using .sheet(item:) and button action not called

import SwiftUI

extension Int: Identifiable {
    public var id: Int { return self }
}

struct ContentView: View {
    @State var shareSheet: Bool = false
    @State var selectedNo : Int?
    
    
    var body: some View {
        VStack {
            ForEach(0..<2, id: \.self, content: { rowNo in
                Text("world \(rowNo)")
                Button(action: {
                    shareSheet.toggle()
                        selectedNo = rowNo
                }) {
                    Text("Go to world\(rowNo)")
                }
            })
            
        }
        .padding()
        .sheet(item: $selectedNo) { row in
// BUG: when replacing 'row' below with 'selectedNo' nil is passed.
                ShareView(message:"Hello \(row)" )
            }
    }
    
    struct ShareView: View {
        let message: String
        var body: some View {
            Text(message)
        }
    }
}

Hi @perbrondum ,

I was unable to reproduce this on Xcode 16.1 Beta or Xcode 16 RC with an iPhone simulator or physical device running iOS 18.0. Can you please tell me what versions you were testing with?

I'm testing with Version 16.1 beta (16B5001e). Just to be clear. When replacing: ShareView(message:"Hello (row)" with: ShareView(message:"Hello (selectedNo)" I would expect to see 'Hello 1 when clicking on 'goto world 1' but I get 'Hello nil' the first time, on subsequent clicks on 'goto world1' it works and I see Hello Optional(1)

SwiftUI bug using .sheet(item:) and button action not called
 
 
Q