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?

.onChange(of:perform:)内で変更前の値を取得する

Last updated at Posted at 2023-09-01

同じボタンがタップされた時は処理をせず、値に変更があった場合のみ処理をしたい

enum Type: String, CaseIterable {
  case hoge, fuga, piyo
}

struct HogeView: View {
  @State private var type: Type = .hoge

  var body: some View {
    ForEach(Type.allCases, id: \.self) { type in
      Button(type.rawValue) {
        self.type = type           
      }
    }
    .onChange(of: type) { [oldValue = type] newValue in
      if oldValue != newValue {
        // do something
      }
    }
  }
}

iOS17で非推奨になった模様

iOS17+

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?