UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.