1
2

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】元Viewを再表示する際に、Viewを再描画し値を更新する方法

Last updated at Posted at 2025-02-27

はじめに

※この投稿はメモ程度の投稿です。
この記事は、SwiftUIでNavigationLinkを利用し、Viewを移動する際に、遷移先のViewでの値の変更を元のViewに反映させるための方法を示します。やり方としてはViewを再描画させる方法です。

ソースコード

@State private var refreshID = UUID()

var body: some View {
    List {
        ForEach(items) { item in
            Text(item.name ?? "No Name")
        }
    }
    .id(refreshID) 
    .onAppear {
        refreshID = UUID()
    }
}

説明

簡単に説明をします

@State private var refreshID = UUID()

上記の部分で、初期のUUIDを指定

 .id(refreshID) 

IDが変わったら新しく再描画する処理

.onAppear {
    refreshID = UUID()
}

再度新しいUUIDを格納する

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?