0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Swift Ui]iPadでNumberのみのtextFieldの実装

Posted at

結論から伝えるとこちら

@State var editNum = "0"

TextField("", text: Binding(
    get: {editNum},
    set: {editNum = $0.filter{"0123456789".contains($0)}}))
.keyboardType(.numberPad)

iPad以外の場合は下記コードでいけるはず

@State var editNum = 0 //←もしかしたらここはStringにしないとダメかも

TextField("Input Number", value: $editNum, format: .number)
    .keyboardType(.numberPad)

numberだけのtextFieldが必要で(iPad以外の場合は下記コードでいけるはず)のコードを試したところ普通にnumber以外のtextが入力できてしまい別の方法ないかググったら結論で伝えたやり方が出てました

忘備録がメインではありますが、誰かの参考になれば幸いです。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?