SwiftUI: モーダル画面に対するEnvironmentを指定する際の注意

p0dee
1 min readJun 1, 2020

--

モーダル画面に対するEnvironment(やEnvironmentObject)を指定においてハマったことがあったのでメモ。

これは、Environmentに設定した文字列を、モーダル画面で表示する単純な例。

これでは、モーダル表示するModalView()にはEnvironmentは指定されない。

Environmentが、environment(_:_:)を実行するビューのもつサブツリー全体に対して指定されるのに対して、sheet(…)で表示するビューは、表示元のサブビューにあたらないからである。

.sheet(… 以下を次のように修正することで、モーダル画面に対して正しくEnvironmentを指定することができる。

ちなみに、NavigationViewに指定したEnvironmentは、NavigationLinkの遷移先に伝播される。

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response