我们如何使用“SwiftUI"添加“Button"和“TextField" [英] How can we add `Button` and `TextField` by using `SwiftUI`

查看:41
本文介绍了我们如何使用“SwiftUI"添加“Button"和“TextField"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 SwiftUI(Apple 为 iOS 13 和 Xcode 11 提供的新框架:Apple 的 SwiftUI).

I am learning SwiftUI (New framework provided by Apple with iOS 13 and Xcode 11 : SwiftUI by Apple).

我想通过操作在 ListView 中添加 ButtonTextField.我希望该用户中的一个文本字段可以添加 1 到 10 之间的任何一个数字,然后点击 SEND 按钮.任何人都知道如何在其中添加按钮以及我们如何使用 SwiftUI 处理 Buttontouch event ?

I want to add Button and TextField in ListView with action. I want one textfield in that user can add any one number from 1 to 10 and then hit SEND button. Anyone have any idea how to add button in it and also how can we handle touch event of Button with SwiftUI ?

任何帮助将不胜感激.

推荐答案

这是一个简单的视图,其中包含一个文本字段和一个水平堆栈中的按钮.

Here is a simple view what contains a textfield and a button in a horizontal stack.

要在您的 Button 中处理用户交互,只需覆盖 action 闭包即可.

To handle the user interaction with in your Button, just overwrite the action closure.

import SwiftUI

struct ButtonAndTextFieldView : View {

    @State var text: String = ""

    var body: some View {
        HStack {
            TextField($text,
                      placeholder: Text("type something here..."))
            Button(action: {
                // Closure will be called once user taps your button
                print(self.$text)
            }) {
                Text("SEND")
            }
        }
    }
}

#if DEBUG
struct ButtonWithTextFieldView_Previews : PreviewProvider {
    static var previews: some View {
        ButtonWithTextFieldView()
    }
}
#endif

这篇关于我们如何使用“SwiftUI"添加“Button"和“TextField"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆