radar
radar

Reputation: 1

TextField action in watchOS without Storyboards....is IBOutlet/IBAction available?

I have to be missing something... in the post-Storyboard world of watchOS (9.x) I cannot figure out what has to be the simplest of things. So I have this simple app that displays 4 numbers, the two lower numbers are editable by the user, and then based on what they enter the other numbers will update. If I had a storyboard I'd be done; as it stands with Xcode 14.3 you seemingly can't use that with watchOS so I'm down the path with SwiftUI. What I THINK I wanted from the storyboard if it still existed was:

  1. ability to tie/bind an @IBOutlet to one of my TextField
  2. similarly then, tie a couple of actions; specifically editingDidBegin and editingDidEnd

To me though, it even seems in the Apple forums they show Swift examples of such, completely ignoring the fact that watchOS has deprecated the ability to use them. Again, I have to be missing something simple....

Here's my code:

    @State private var LowerLeft: String = "12"
    @State private var UpperLeft: String = "10"
    @State private var UpperRight: String = "11"
    @State private var LowerRight: String = "14"

struct ContentView: View {
   var body: some View {
            VStack(alignment: .center) {
                HStack {
                    VStack {
                        Text("\(UpperLeft)")
                            .font(.title)
                            .fixedSize()
                        TextField("", text: $LowerLeft)
                            .font(.title)
                            .fixedSize()
                    } // VSTACK left
                    VStack {
                        Text("\(UpperRight)")
                            .font(.title)
                            .fixedSize()
                        TextField("", text: $LowerRight)
                            .font(.title)
                            .fixedSize()
                    } // VSTACK right
                }  // HSTACK
          } // VSTACK
} // VIEW

So notice the two lower entities are TextField blocks; as this is seemingly the only way to get watchOS to pull up a keyboard to allow them to be edited. The glaring problems I've run into and found no example of in this post-storyboard world of watchOS are:

  1. I want to clear the text when the user starts editing (used to be an option directly in storyboard, thinking I might be able to fake it with editingDidBegin @IBAction... but keep reading)
  2. I need to be able to 'adjust' the value the user typed in after they update it. So again, I used to do this by typing this to an @IBAction with editingDidEnd; but I would do it graphically in the storyboard.

But even when I occasionally did it without the storyboard in just code...I knew what it was named (because I typed it in the storyboard editor!) so I could do the binding to my TextField... here I don't see the name/ID of my textfield. (and just to avoid the "why are you trying to change what the user typed?" questions; it's twofold. Firstly, I haven't figured out how to get watchOS to display a decimal keypad as the entries are actually only numbers so if the user puts in a space or something stupid I have to fix it, and then secondly because they are numbers there's a floor/ceiling I need to apply in addition to the other cleanup)

So the goal is:

  1. entry clears on begin editing. I'm thinking I could do this with editingDidBegin, though candidly even if I get that to work I'm not sure the OS won't have grabbed the value pre-execution of such, so here I state the GOAL because if you have a better way, I'm all ears

  2. be notified that one/both of the lower value got updated, so I can recalculate and redisplay all 4 numbers shown. Pretty sure this one maps well to editingDidEnd, IF I could figure out a way to connect that to my two TextField things. Again, if I'm totally down an 'old school' path and there's some new cool way to do this, please advise (as a note, I tried a Button instead...seemed cool because you can add .action right inside the stack; but Buttons don't seemingly have a way to pull up the keyboard... spinning my wheels)

Couple of things I tried, there is an extension .clearsOnBeginEditing which sounds awesome in name; though when I put it on the TextField (I tried right after the .fixedSize()) it tells me that "Value of type some View doesn't have that member...quite odd because it's being attached to the TextField, not the view.

                    VStack {
                        Text("\(UpperRight)")
                            .font(.title)
                            .foregroundColor(Color.yellow)
                            .multilineTextAlignment(.trailing)
                            .padding(.bottom, 0.0)
                            .padding(.leading, 10.0)
                            .fixedSize()
                        TextField("", text: $LowerRight)
                            .padding(.top, 0.0)
                            .padding(.leading, 10.0)
                            .font(.title)
                            .foregroundColor(Color.yellow)
                            .fixedSize()
                            .clearsOnBeginEditing()   X Value of type 'some View' has no member...blah blah blah
                    } // VSTACK right

Very confusing, hope someone can help because every video I've perused on watchOS development is using things like Lists and pre-built Apple things; so never seems to address what I think must be so basic I've just missed step 1.

Upvotes: 0

Views: 144

Answers (0)

Related Questions