0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SwiftUI TextFieldでreturnキーを押した時に処理を行う方法

Posted at

TextFieldで入力returnキーを押した時に処理を行う方法です

サンプルコード

TextFieldOnCommit.swift
struct TextFieldOnCommit: View {
    @State private var inputText: String = ""
    @State private var showText: String = ""
    
    var body: some View {
        VStack(alignment: .center, spacing: 12) {
            TextField("ここに入力",
                      text: $inputText,
                      onCommit: {showText = inputText}
            )
            Text(showText)
        }
    }
}

onCommit: {showText = inputText}の部分がreturnキーを押した時になります。
今回の場合は入力したテキストを別のテキストに表示する処理をしております。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?