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?

【SwiftUI】Textを入力できるAlertを実装する

Posted at

概要

  • TextFieldの機能を備えたAlertを実現する

やること

  • 通常の.alertモディファイアを用意しておく(.alert周りはやることが変わらないので説明を割愛)
  • 入力された値を保持するための@State Boolを用意しておく。
  • TextFieldを用いて変数の中に入力値を保持しておく。
  • オブジェクトの更新処理をプライマリの中で発火させる。

コード

 .alert("", isPresented: $showAlert) {
        TextField("更新された値を入力", text: $editedText)
        Button("更新") {
           // オブジェクトの値を更新する処理(省略)
            showAlert = false
        }
        Button("キャンセル", role: .cancel) {
            showAlert = false
        }
    } message: {
        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?