SwiftUI App crash after keyboard show

This is my code with swiftui, when I click button => change tradingMode inside sheet => after show keyboard from ContentView => app crash. Please help me and thanks!

Code Block
enum TradingMode {
  case Derivatives, Equities
}
struct ContentView: View {
  @State var tradingMode : TradingMode = TradingMode.Equities
  @State var isShowSecondView = false
   
  var body: some View {
    VStack(content: {
      Button("show second view") {
        isShowSecondView.toggle()
      }
      TabView {
        switch tradingMode {
        case .Equities:
          VStack(content: {
            Text("Tab 1 Un")
              .padding()
            TextField("ple", text: .constant(""))
          })
          .tabItem {
            Text("tab 1")
          }.tag(0)
           
        case .Derivatives:
          VStack(content: {
            Text("Tab 1 Der")
              .padding()
            TextField("ple", text: .constant(""))
          })
          .tabItem {
            Text("tab 1")
          }.tag(0)
        }
         
        switch tradingMode {
        case .Equities:
          VStack(content: {
            Text("Tab 2 Un")
              .padding()
            TextField("ple", text: .constant(""))
          })
          .tabItem {
            Text("tab 2")
          }.tag(1)
        case .Derivatives:
          VStack(content: {
            Text("Tab 2 Der")
              .padding()
            TextField("ple", text: .constant(""))
          })
          .tabItem {
            Text("tab 2")
          }.tag(1)
        }
      }
       
    })
    .sheet(isPresented: $isShowSecondView, content: {
      SecondView(tradingMode: $tradingMode)
    })
    
  }
}
struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
      .previewLayout(.device)
       
       
  }
}
struct SecondView: View {
  @Environment(\.presentationMode) var presentation
  @Binding var tradingMode : TradingMode
   
  var body: some View {
    VStack(content: {
      Button("Change state") {
        if tradingMode == .Derivatives {
          tradingMode = .Equities
        } else {
          tradingMode = .Derivatives
        }
        self.presentation.wrappedValue.dismiss()
      }
    })
    
  }
}

and video bug
drive.google.com/file/d/18eXCmlByGqJylEhCZbtJ4Rn0wmI0bb/view
Answered by OOPer in 673012022
You may have tried already (and this may be difficult to apply to your actual app), but this is the only workaround I could have found till now:
Code Block
struct ContentView: View {
@State var tradingMode : TradingMode = TradingMode.Equities
@State var isShowSecondView = false
var body: some View {
VStack(content: {
Button("show second view") {
isShowSecondView.toggle()
}
TabView {
switch tradingMode {
case .Equities:
VStack(content: {
Text("Tab 1 Un")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 1")
}.tag(0)
VStack(content: {
Text("Tab 2 Un")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 2")
}.tag(1)
case .Derivatives:
VStack(content: {
Text("Tab 1 Der")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 1")
}.tag(0)
VStack(content: {
Text("Tab 2 Der")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 2")
}.tag(1)
}
}
})
.sheet(isPresented: $isShowSecondView, content: {
SecondView(tradingMode: $tradingMode)
})
}
}


Video is inaccessible.

When running your code, just get a screen with
  • show second view at top

  • Tab 1 Un in the middle

  • a field on the left at the middle, but quasi hidden

  • a tab bar at bottom

when I click button

Which button, please be more precise

I clicked on secondView
  • only change state button

  • which returns to the first view, just changed with Tab 1 Der in the middle

Impossible to test anything.
As far as I tried your code, crash occurs before keyboard shown:
  • Tap show second view

  • Tap Change state

  • Tap tab 2

Can you clarify how to reproduce your issue?
Code Block
drive.google.com/file/d/18eXCmlByGqJylE_hCZbtJ4Rn0wmI0bb_/view?usp=sharing

this my video bug @OOPer @Claude31

this my video bug @OOPer @Claude31

If you cannot provide issue-reproducible code and detailed steps to reproduce the issue, there is nothing more I can do for you.
Hope your issue would be solved soon. Good luck.
OK, I could reproduce the crash (after deselecting I/O > Keyboard > Connect Hardware Keyboard).

The crash seems due to the 2         switch tradingMode { } section
If I comment out any of them, no more crash.

If I reconnect Hardware Keyboard; no crash either.

So problem is effectively caused by showing keyboard.
Some conflict with the tabs ?

I also get a crash if I tap on tab 2, without any keyboard.

So there is a problem with tabs.

I'll look further to it.

I run Xcode 12.4 on MacOS 10.15.7
@claude31: yep, but, have you solution for my case?
@OOPer, this case like my video. open app -> click show second view -> click change Change state -> focus to "ple" TextField -> show keyboard and crash.

my code in first post and you can run it.

I run with xcode 12.4 and ios 14.4

@OOPer, this case like my video. open app -> click show second view -> click change Change state -> focus to "ple" TextField -> show keyboard and crash.

Thanks for showing the steps. I could reproduce the issue with doing as instructed.

In my opinion, this is a bug of the current implementation of SwiftUI.
But you would not be able to wait until this issue is fixed by Apple, and may need sort of workarounds.

In fact, I was searching for some workarounds for the steps I have shown, no avail till now.
I will show you when I find some workaround which works steadily enough.
Accepted Answer
You may have tried already (and this may be difficult to apply to your actual app), but this is the only workaround I could have found till now:
Code Block
struct ContentView: View {
@State var tradingMode : TradingMode = TradingMode.Equities
@State var isShowSecondView = false
var body: some View {
VStack(content: {
Button("show second view") {
isShowSecondView.toggle()
}
TabView {
switch tradingMode {
case .Equities:
VStack(content: {
Text("Tab 1 Un")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 1")
}.tag(0)
VStack(content: {
Text("Tab 2 Un")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 2")
}.tag(1)
case .Derivatives:
VStack(content: {
Text("Tab 1 Der")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 1")
}.tag(0)
VStack(content: {
Text("Tab 2 Der")
.padding()
TextField("ple", text: .constant(""))
})
.tabItem {
Text("tab 2")
}.tag(1)
}
}
})
.sheet(isPresented: $isShowSecondView, content: {
SecondView(tradingMode: $tradingMode)
})
}
}


In your initial code, you set tag(0) both line 24 and 34.

That may be the confusion ?

Change line 34 with tag(1).

Similarly, change line 46 to tag(0)
@Claude31: no, I want tag 0 is tab 1 and tag 1 is tab 2. You can run my code. line 24 and 34 can not confusion with tradingMode.
@OOPer: SwiftUI not good like a my dream :D

SwiftUI not good like a my dream :D

I agree. You can send bug reports/feature requests using Apple's Feedback assistant.


SwiftUI App crash after keyboard show
 
 
Q