首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WatchOS6的SwiftUI中获取scribble输入

如何在WatchOS6的SwiftUI中获取scribble输入
EN

Stack Overflow用户
提问于 2019-11-18 13:24:16
回答 1查看 481关注 0票数 1

我正在尝试这样做,这样当用户点击里面有注释的按钮时,它将弹出一个涂鸦,用户输入和口述。我看了一些其他的解决方案,但我不能让它们中的任何一个工作。我认为这可能是因为它们都是在watchOS3中实现的建议。我还查看了苹果的文档,找到了这个https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1619527-presenttextinputcontroller,但我无法让它正常工作。我有我的完整代码如下

代码语言:javascript
复制
import SwiftUI
import Foundation

struct ContentView: View {
    @State var counterValues = [0,0,0, -1, -1, -1, -1, -1, -1, -1 ]
    @State var counterNames = ["Pullups", "Pushups", "Situps", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
    @State var counters = 3;

    var body: some View {
       ScrollView {       
            VStack {
                ForEach( counterValues.indices ) { index in
                    if (self.counterValues[index]>=0) {
                        Text("\(self.counterNames[index])")
                        Button(action: {
                            self.counterValues[index] += 1
                        }) {
                            Text("\(self.counterValues[index])")
                                .font(.title)
                                .multilineTextAlignment(.center)
                        }
                    }
                }

                Button(action: {                   
                       //Add user input here
                }) {
                    Image(systemName: "plus")
                        .font(.largeTitle)
                        .foregroundColor(.green)
                }
                .cornerRadius(40)
                    .shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
                    .padding(20)

            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

这是一个特定的按钮,当单击它时,我想让它允许用户输入,然后将输入存储在一个变量中

代码语言:javascript
复制
                Button(action: {
                    //Add user input here
                }) {
                    Image(systemName: "plus")
                        .font(.largeTitle)
                        .foregroundColor(.green)
                }
                .cornerRadius(40)
                    .shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )
                    .padding(20)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-18 15:01:23

您可以尝试使用textfield执行以下代码。

代码语言:javascript
复制
           struct ContentView: View {

            @State
               var counterValues = [0,0,0, -1, -1, -1, -1, -1, -1, -1 ]
               @State var counterNames = ["Pullups", "Pushups", "Situps", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
               @State var counters = 3;
               @State var textInput = "";
               @State var showInput = false;
               var body: some View {



                   ScrollView {


                       VStack {



                           ForEach( counterValues.indices ) { index in
                               if (self.counterValues[index]>=0) {

                           Text("\(self.counterNames[index])")
                           Button(action: {
                               self.counterValues[index] += 1
                           }) {
                               Text("\(self.counterValues[index])")
                               .font(.title)
                               .multilineTextAlignment(.center)
                           }
                               }
                       }

                           Button(action: {


                                  //Add user input here
                            self.showInput.toggle()

                               }) {
                                   Image(systemName: "plus")
                                   .font(.largeTitle)
                                   .foregroundColor(.green)


                               }
                               .cornerRadius(40)

                           .shadow(color: .gray, radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/ )

                           .padding(20)
                        if self.showInput{
                            TextField.init("input", text: $textInput)}

                    }
               }
               }
            }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58908414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档