GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Prevent user from tapping button while entering data

Forums > SwiftUI

Here's a snippet, iPad specific. If the user clicks the button while editing the TextField (cursor flashing) in dataPtView, the app crashes.

CellView:

VStack{
  Button("TagOut"){
    self.tagOut.toggle()
  }

  if self.tagOut {
    TagOutView(question: question)
  }

  if !self.tagOut{
    if question.type == "Y/N"{
      YesOrNoView(question: question)
    } else if question.type == "DataPt"{
      DataPtView(question: question)
    } else {
      RecordEntryView()
  }
  ...

DataPtView:

 ...
 TextField("Data: ", text: $collectedData)
        .onReceive(Just(collectedData)) {value in
          let filtered = value.filter {"01234567890-".contains($0)}
          if filtered != value{
            self.invalidCollectedData = true
          } else {
            self.invalidCollectedData = false
          }
  }
  ...

I'm also using an AdaptsToKeyboard ViewModifier for when CellView is covered by the keyboard.https://stackoverflow.com/questions/56491881/move-textfield-up-when-the-keyboard-has-appeared-in-swiftui

How do I prevent this from happening? If user hides the keyboard before clicking the button, everything is fine, but that isn't intuitive.

3      

Answered on stack overflow. Used keyboardHeight to hide the button. Ingenious!

https://stackoverflow.com/questions/62902028/swiftui-prevent-user-from-tapping-button-while-entering-data

3      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.