Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftUI で複数のアラート表示を管理する
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Yutaka
February 19, 2025
130
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SwiftUI で複数のアラート表示を管理する
Yutaka
February 19, 2025
More Decks by Yutaka
See All by Yutaka
Swift 6 の地味な (?) アップデート
tajitaji
1
430
2018.01.19 すくすく子育てエンジニア Meetup #1
tajitaji
1
2.2k
mlmodel のコンパイル
tajitaji
0
1.8k
統計・マーケ・R/Python・機械学習 Meetup! #2 2017.10.11
tajitaji
0
200
Vapor プロジェクトの開発に使えそうなツールの紹介
tajitaji
0
500
Server Side Swift, Vapor を触ってみた
tajitaji
0
2k
SwiftでのError Handlingを学び直す!
tajitaji
3
820
Featured
See All Featured
Chasing Engaging Ingredients in Design
codingconduct
0
210
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
How to build a perfect <img>
jonoalderson
1
5.6k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
210
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
22k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Amusing Abliteration
ianozsvald
1
200
WCS-LA-2024
lcolladotor
0
620
Utilizing Notion as your number one productivity tool
mfonobong
4
320
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Transcript
:VUBLB5BKJLB 4XJGU6*Ͱ ෳͷΞϥʔτදࣔΛཧ͢Δ 
ࣗݾհ w ଟࣛ๛ 5BKJLB:VUBLB w J04ΞϓϦΤϯδχΞ w גࣜձࣾ'0-*0 w
!:VU@5BK w !UBKJUBKJ
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented
= false var body: some View { Button("Alert") { isPresented = true } .alert("Alert", isPresented: $isPresented) { Button("Close", role: .cancel) {} } } }
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented
= false var body: some View { Button("Alert") { isPresented = true } .alert("Alert", isPresented: $isPresented) { Button("Close", role: .cancel) {} } } } !4UBUFͳ#PPMΛ ද੍ࣔޚϑϥάͱͯ͠อ࣋
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented
= false var body: some View { Button("Alert") { isPresented = true } .alert("Alert", isPresented: $isPresented) { Button("Close", role: .cancel) {} } } } #JOEJOH#PPMܕͰ͋Δ JT1SFTFOUFEΛ͢
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented1
= false @State var isPresented2 = false var body: some View { VStack { Button("Alert 1") { isPresented1 = true } Button("Alert 2") { isPresented2 = true } } .alert("Alert 1", isPresented: $isPresented1) { Button("Close", role: .cancel) {} } .alert("Alert 2", isPresented: $isPresented2) { Button("Close", role: .cancel) {} } } }
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented1
= false @State var isPresented2 = false var body: some View { VStack { Button("Alert 1") { isPresented1 = true } Button("Alert 2") { isPresented2 = true } } .alert("Alert 1", isPresented: $isPresented1) { Button("Close", role: .cancel) {} } .alert("Alert 2", isPresented: $isPresented2) { Button("Close", role: .cancel) {} } } } ͦΕͧΕͷΞϥʔτʹରԠ͢Δ ද੍ࣔޚϑϥάΛ࣋ͭ
🎉
ຊʹͪΌΜͱ Ξϥʔτͷදࣔඇදࣔঢ়ଶΛ දݱͰ͖ͯΔɻɻʁ
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented1
= false @State var isPresented2 = false var body: some View { VStack { Button("Alert 1") { isPresented1 = true } Button("Alert 2") { isPresented2 = true } } .alert("Alert 1", isPresented: $isPresented1) { Button("Close", role: .cancel) {} } .alert("Alert 2", isPresented: $isPresented2) { Button("Close", role: .cancel) {} } } } isPresented1 isPresented2 false false ͲͪΒඇදࣔ true false "MFSU͕දࣔ false true "MFSU͕දࣔ true true "MFSUͱ"MFSU͕දࣔ ʢʁʁʁʣ
4XJGU6*ͰͷΞϥʔτදࣔ ͭͷ߹ struct ContentView: View { @State var isPresented1
= false @State var isPresented2 = false var body: some View { VStack { Button("Alert 1") { isPresented1 = true } Button("Alert 2") { isPresented2 = true } } .alert("Alert 1", isPresented: $isPresented1) { Button("Close", role: .cancel) {} } .alert("Alert 2", isPresented: $isPresented2) { Button("Close", role: .cancel) {} } } } isPresented1 isPresented2 false false ͲͪΒඇදࣔ true false "MFSU͕දࣔ false true "MFSU͕දࣔ true true "MFSUͱ"MFSU͕දࣔ ʢʁʁʁʣ ࣮ࡍʹೋͭͷΞϥʔτ͕ಉ࣌ʹ දࣔ͞ΕΔ͜ͱͳ͍ɻ ͕ɺೋͭͷΞϥʔτ͕දࣔ͞Εͯ ͍ΔΑ͏ͳදݱʹݟ͑Δɻ
վળͯ͠ΈΔ
4XJGU6*ͰͷෳΞϥʔτදࣔ struct ContentView: View { @State var alert: MyAlert?
var body: some View { VStack { Button("Alert 1") { alert = .alert1 } Button("Alert 2") { alert = .alert2 } } .alert("Alert 1", isPresented: Binding( get: { alert == .alert1 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } .alert("Alert 2", isPresented: Binding( get: { alert == .alert2 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } } } enum MyAlert: Equatable { case alert1, alert2 } &OVNΛར༻ͯ͠ ΞϥʔτͷछྨΛఆٛ
4XJGU6*ͰͷෳΞϥʔτදࣔ struct ContentView: View { @State var alert: MyAlert?
var body: some View { VStack { Button("Alert 1") { alert = .alert1 } Button("Alert 2") { alert = .alert2 } } .alert("Alert 1", isPresented: Binding( get: { alert == .alert1 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } .alert("Alert 2", isPresented: Binding( get: { alert == .alert2 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } } } enum MyAlert: Equatable { case alert1, alert2 }
4XJGU6*ͰͷෳΞϥʔτදࣔ struct ContentView: View { @State var alert: MyAlert?
var body: some View { VStack { Button("Alert 1") { alert = .alert1 } Button("Alert 2") { alert = .alert2 } } .alert("Alert 1", isPresented: Binding( get: { alert == .alert1 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } .alert("Alert 2", isPresented: Binding( get: { alert == .alert2 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } } } enum MyAlert: Equatable { case alert1, alert2 } Ξϥʔτͷදࣔঢ়ଶΛ 0QUJPOBMΛͬͯදݱ alert .none ͲͪΒඇදࣔ .some(.alert1) "MFSU͕දࣔ .some(.alert2) "MFSU͕දࣔ
4XJGU6*ͰͷෳΞϥʔτදࣔ struct ContentView: View { @State var alert: MyAlert?
var body: some View { VStack { Button("Alert 1") { alert = .alert1 } Button("Alert 2") { alert = .alert2 } } .alert("Alert 1", isPresented: Binding( get: { alert == .alert1 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } .alert("Alert 2", isPresented: Binding( get: { alert == .alert2 }, set: { if !$0 { alert = nil } } )) { Button("Close", role: .cancel) { alert = nil } } } } enum MyAlert: Equatable { case alert1, alert2 } #JOEJOH#PPMΛ ࣗલͰ࡞Δ
Ћ
4XJGU6*ͰͷෳΞϥʔτදࣔ struct ContentView: View { @State var alertState: AlertState<MyAlert>
= .dismissed var body: some View { VStack { Button("Alert 1") { alertState = .presenting(.alert1) } Button("Alert 2") { alertState = .presenting(.alert2) } } .alert("Alert 1", isPresented: Binding( get: { alertState == .presenting(.alert1) }, set: { if !$0 { alertState = .dismissed } } )) { Button("Close", role: .cancel) { alertState = .dismissed } } .alert("Alert 2", isPresented: Binding( get: { alertState == .presenting(.alert2) }, set: { if !$0 { alertState = .dismissed } } )) { Button("Close", role: .cancel) { alertState = .dismissed } } } } enum MyAlert: Equatable { case alert1, alert2 } 0QUJPOBMͷΘΓʹ Ξϥʔτͷදࣔঢ়ଶΛ ද͢ઐ༻ͷܕΛ༻ҙ enum AlertState<Alert> where Alert: Equatable { case dismissed case presenting(Alert) }
4XJGU6*ͰͷෳΞϥʔτදࣔ struct ContentView: View { @State var alertState: AlertState<MyAlert>
= .dismissed var body: some View { VStack { Button("Alert 1") { alertState = .presenting(.alert1) } Button("Alert 2") { alertState = .presenting(.alert2) } } .alert("Alert 1", isPresented: Binding( get: { alertState == .presenting(.alert1) }, set: { if !$0 { alertState = .dismissed } } )) { Button("Close", role: .cancel) { alertState = .dismissed } } .alert("Alert 2", isPresented: Binding( get: { alertState == .presenting(.alert2) }, set: { if !$0 { alertState = .dismissed } } )) { Button("Close", role: .cancel) { alertState = .dismissed } } } } enum MyAlert: Equatable { case alert1, alert2 } 0QUJPOBMͷΘΓʹ Ξϥʔτͷදࣔঢ়ଶΛ ද͢ઐ༻ͷܕΛ༻ҙ enum AlertState<Alert> where Alert: Equatable { case dismissed case presenting(Alert) } w 0QUJPOBMͷҥߏจ͕͑ͳ ͍ͷͰهड़ྔ૿͑Δ w จ຺ΘΓ͘͢ͳͬͨ
ڞ௨Խͯ͠ΈΔ
4XJGU6*ͰͷෳΞϥʔτදࣔͷڞ௨Խ struct ContentView: View { @State var alertState: AlertState<MyAlert>
= .dismissed var body: some View { VStack { Button("Alert 1") { alertState = .presenting(.alert1) } Button("Alert 2") { alertState = .presenting(.alert2) } } .alert("Alert 1", isPresented: Binding( get: { alertState == .presenting(.alert1) }, set: { if !$0 { alertState = .dismissed } } )) { Button("Close", role: .cancel) { alertState = .dismissed } } .alert("Alert 2", isPresented: Binding( get: { alertState == .presenting(.alert2) }, set: { if !$0 { alertState = .dismissed } } )) { Button("Close", role: .cancel) { alertState = .dismissed } } } } enum MyAlert: Equatable { case alert1, alert2 } ͜͜ͷ#JOEJOHΛຖճ ॻ͔ͳ͍ͱ͍͚ͳ͍ͷ ͪΐͬͱ໘ enum AlertState<Alert> where Alert: Equatable { case dismissed case presenting(Alert) }
4XJGU6*ͰͷෳΞϥʔτදࣔͷڞ௨Խ protocol AlertCase: Equatable {} protocol AlertStateRepresentable: Equatable {
associatedtype Alert: AlertCase static var dismissed: Self { get } static func presenting(_ alert: Alert) -> Self } enum AlertState<Alert: AlertCase>: AlertStateRepresentable { case dismissed case presenting(Alert) } extension Binding where Value: AlertStateRepresentable { func isPresented(_ target: Value.Alert) -> Binding<Bool> { .init( get: { wrappedValue == .presenting(target) }, set: { if !$0 { wrappedValue = .dismissed }} ) } } ͪΐͬͱίʔυΛՃ
4XJGU6*ͰͷෳΞϥʔτදࣔͷڞ௨Խ protocol AlertCase: Equatable {} protocol AlertStateRepresentable: Equatable {
associatedtype Alert: AlertCase static var dismissed: Self { get } static func presenting(_ alert: Alert) -> Self } enum AlertState<Alert: AlertCase>: AlertStateRepresentable { case dismissed case presenting(Alert) } extension Binding where Value: AlertStateRepresentable { func isPresented(_ target: Value.Alert) -> Binding<Bool> { .init( get: { wrappedValue == .presenting(target) }, set: { if !$0 { wrappedValue = .dismissed }} ) } } #JOEJOHʹ JT1SFTFOUFEΛੜͯ͠ #JOEJOH#PPMม
4XJGU6*ͰͷෳΞϥʔτදࣔͷڞ௨Խ struct ContentView: View { @State var alertState: AlertState<MyAlert>
= .dismissed var body: some View { VStack { Button("Alert 1") { alertState = .presenting(.alert1) } Button("Alert 2") { alertState = .presenting(.alert2) } } .alert("Alert 1", isPresented: $alertState.isPresented(.alert1)) { Button("Close", role: .cancel) { alertState = .dismissed } } .alert("Alert 2", isPresented: $alertState.isPresented(.alert2)) { Button("Close", role: .cancel) { alertState = .dismissed } } } } enum MyAlert: AlertCase { case alert1, alert2 } ֤ը໘Ͱ#JOEJOH#PPMΛ࡞Βͳͯ͘ྑ͍
ৄ͘͠ϒϩά w IUUQTUBKJUBKJIBUFOBCMPHDPNFOUSZ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠🙇