Why You Should Use The SwiftUI Coordinator Pattern

  Переглядів 12,023

Rebeloper - Rebel Developer

Rebeloper - Rebel Developer

День тому

Swift Coordinator Pattern
SwiftUI Camp 👉 store.rebeloper.com/swiftui-c...
In this video, we'll be introducing the Swift Coordinator Pattern. This pattern is used to manage the life cycle of navigation in a program. We'll walk you through the different aspects of the pattern and how to use it in your code.
If you're interested in learning more about the Swift language and how to write better code, then this video is for you! By the end of this video, you'll have a better understanding of the Swift Coordinator Pattern and how to use it in your SwiftUI projects.
Don’t forget to download the resources:
→ store.rebeloper.com/youtube-c...
CHAPTERS:
_____
00:00 INTRO
00:34 SWIFTUI CAMP
01:00 FINAL PROJECT DEMO
02:11 STARTER PROJECT
02:40 COORDINATOR
03:18 COORDINATOR PAGE
04:12 COORDINATOR SHEET
04:30 COORDINATOR FULL SCREEN COVER
04:50 COORDINATOR CLASS
05:55 PUSH WITH THE COORDINATOR PATTERN
06:23 PRESENT A SHEET WITH THE COORDINATOR
06:46 PRESENT A FULL SCREEN COVER IN SWIFTUI
07:12 POP WITH THE COORDINATOR PATTERN
07:30 POP TO ROOT IN SWIFTUI
07:52 DISMISS A SHEET WITH THE COORDINATOR
08:05 DISMISS A FULL SCREEN COVER IN SWIFTUI
08:40 BUILD VIEWS FOR THE COORDINATOR PATTERN
10:15 COORDINATOR VIEW
11:00 NAVIGATION STACK WITH THE COORDINATOR
12:00 SHEETS IN THE COORDINATOR PATTERN
12:33 FULL SCREEN COVER WITH COORDINATOR
13:20 PUSH A VIEW WITH THE COORDINATOR PATTERN
14:53 PRESENT A SHEET IN SWIFTUI AND COORDINATOR
15:12 PRESENT A FULL SCREEN COVER WITH COORDINATOR
15:25 DISMISS A MODAL WITH THE COORDINATOR PATTERN
15:58 SHOWING THE NAVIGATION TITLE IN SWIFTUI
16:54 PUSH AND POP WITH THE COORDINATOR
17:48 POP TO ROOT IN SWIFTUI AND COORDINATOR PATTERN
18:17 WHERE TO GO NEXT
**VIDEOS I RECOMMEND**
→ PLAYLIST:
Senior iOS Developer Secret:
• 5 Levels of SwiftUI Sk...
DO YOU WANT TO BE MY APPRENTICE?
HIRE ME → rebeloper.com/hire-us/
TOOLS I RECOMMEND:
→ rebeloper.com/tools
__________
Get in touch:
→ support@rebeloper.com
SUBSCRIBE to weekly tips & tutorials for building iOS apps!
→ ukposts.info?su...
GitHub: github.com/rebeloper/
Hire me: rebeloper.com/hire-us/
LinkedIn: / rebeloper
My Blog: rebeloper.com/blog
Follow me on Instagram: / rebeloper
______
#rebeloper

КОМЕНТАРІ: 63
@rebeloper
@rebeloper Рік тому
Firebase Firestore in SwiftUI Demystified 👉 ukposts.infozSuEvcXbBTw?feature=share
@YourLaw
@YourLaw 10 місяців тому
I have watched a lot of your videos, this is your best video. Clean crisp explanation of a topic.
@rebeloper
@rebeloper 10 місяців тому
Glad you liked it. Next learn about @Observable @Bindable in iOS17 - SwiftUI Data Flow 👇 ukposts.info/have/v-deo/gZeToJ1ummiFkqM.html&ab_channel=Rebeloper-RebelDeveloper
@davidcbeaudoin
@davidcbeaudoin Рік тому
I'm very much a SwiftUI beginner, so I'm still working on wrapping my head around much of the basics. This is a great, easy to understand approach to navigation. Thank you for the great tutorial!
@rebeloper
@rebeloper Рік тому
Glad it was helpful! If you want to boost your learning check out SwiftUI Camp: store.rebeloper.com/swiftui-camp-habit-tracker-edition?coupon=UKposts
@stevenkirke
@stevenkirke Місяць тому
Much appreciated for the very explanatory tutorial!
@rebeloper
@rebeloper Місяць тому
You're very welcome! Next watch: SwiftUI Text - EVERYTHING You Need to Know 👇 ukposts.info/have/v-deo/qnt2mqlsoWlyr3k.html&ab_channel=Rebeloper-RebelDeveloper
@bas3485
@bas3485 Рік тому
Nice video! I like the idea. Do you also have videos about navigation for lower deployment targets? I could use some inspiration
@rebeloper
@rebeloper Рік тому
Yes I do! Check out my store: store.rebeloper.com Let me show you a Progress Button in SwiftUI 👇 ukposts.info/have/v-deo/o3ZphaFpb2N3p4k.html
@user-uk1db9tm2g
@user-uk1db9tm2g 7 місяців тому
Any chance you've found a fix the white flash this navigation system now produces in iOS17? Prior releases of iOS worked perfectly, but now there's an annoying white flash on any pop(). Been messing around with several alternatives to update the background color, but have been unsuccessful so far.
@rebeloper
@rebeloper 7 місяців тому
Must wait for an iOS update for this fix :( Next watch -> From Boring to Breathtaking: Mastering SwiftUI Splash Screens for Remarkable App Launches 👇 ukposts.info/have/v-deo/pHJ0pK2Qe6Kb2H0.html&ab_channel=Rebeloper-RebelDeveloper
@russel_dev
@russel_dev Місяць тому
Nice video. But some tips, in Coordinator View & ViewModel should also be coupled tightly.
@rebeloper
@rebeloper Місяць тому
Thanks for the tip. What do you think about this one: ukposts.info/have/v-deo/q5mcdndwpWt5k58.html
@DragunXtreme
@DragunXtreme 9 місяців тому
How I can set parameters in a View using this pattern? I like very much how you control the navigation is simple and direct, but the views that you use in this example don't receive any argument and I don't no how adapter your Enum Page to receive some parameters depends of View called. But congratulations for the video, your explanation is wonderful
@rebeloper
@rebeloper 9 місяців тому
You can add a dependency on the enum value. I can show you how on a mentoring session here: rebeloper.com/mentoring Next check out SwiftUI Navigation Perfected: Harnessing NStack's Game-changing Capabilities 👇 ukposts.info/have/v-deo/pqKlpmdroIWbkXk.html&ab_channel=Rebeloper-RebelDeveloper
@AppDeveloperPoint
@AppDeveloperPoint Рік тому
Thanks for this video. I have one question here, How you will manage the switch case fro large complex app when you need to route to say 50 screens. ?
@rebeloper
@rebeloper Рік тому
This can be used for 5 or 50 screens too. Number of screens does not matter. Let me show you a Progress Button in SwiftUI 👇 ukposts.info/have/v-deo/o3ZphaFpb2N3p4k.html
@AppDeveloperPoint
@AppDeveloperPoint Рік тому
@@rebeloper yes we can write 100 cases. But will that be a good practice?
@rebeloper
@rebeloper Рік тому
I don't see why it could be a bad practice. BTW check out my lates video 👉 2023's Must-Have Xcode Extensions: Revealed Here! 👇 ukposts.info/have/v-deo/bGFlbYisa35lw30.html
@star_maze
@star_maze Рік тому
@@AppDeveloperPoint in that case you need to consider the modular approach to your architecture where each module will have its coordinator. The global coordinator will handle routes between the modules.
@NeerajGurjar-dx1rr
@NeerajGurjar-dx1rr 9 місяців тому
It is very helpful but what if we need to send data from one view to another view how can we do this. can you help
@rebeloper
@rebeloper 8 місяців тому
Here you go: Dependency Injection in SwiftUI, the Best Pattern: ukposts.info/have/v-deo/hJSjmm5osGpi0as.html&ab_channel=Rebeloper-RebelDeveloper
@AdelUI
@AdelUI 8 місяців тому
First of all, thanx for amazing contents , however, what's the different between coordinator pattern & Routes ?
@rebeloper
@rebeloper 8 місяців тому
In my view no difference between them. It's just Coordinator sound more techy :P BTW have you seen Firebase Login in SwiftUI: Step by Step Masterclass 👇 ukposts.info/have/v-deo/cZyDi5esbGedqKs.html&ab_channel=Rebeloper-RebelDeveloper
@sami790311
@sami790311 9 місяців тому
I like the example. However, there is one thing that still creates a strong coupling between views. Despite the fact that the coordinator is taking care of the navigation, the views are still knowing about other views. What if from BananaView I want to present different views depending on the case? The views should be agnostic about other views. This way they can be reused. I think adding to the coordinator pattern the composition pattern could solve the problem.
@rebeloper
@rebeloper 8 місяців тому
Thank you for the feedback. What do you think of the next video: Dependency Injection in SwiftUI, the Best Pattern: ukposts.info/have/v-deo/hJSjmm5osGpi0as.html&ab_channel=Rebeloper-RebelDeveloper
@alexthurston9940
@alexthurston9940 8 місяців тому
This is a problem with SwiftUI since we need to type the view hierarchy for performance reasons. I’m not sure of any way around it beside AnyView which is bad for performance. Maybe someday! Otherwise you can use UIKit for navigation under the hood.
@Hgvpuncher
@Hgvpuncher Місяць тому
@@alexthurston9940 Why do you think AnyView is bad for perfomance? AnyView is fine for a small project
@cagrgider8320
@cagrgider8320 Рік тому
That looks clean, thanks a lot. But how can we pass a value to the following view with this pattern?
@rebeloper
@rebeloper Рік тому
Good question! For that you might want to check out NavigationRouter: store.rebeloper.com/navigation-router Or SwiftUI Camp (50% OFF discount in the link): store.rebeloper.com/swiftui-camp-twitter-edition?coupon=UKposts Expires January the 31st
@HINDISE_ENGLISHTAK
@HINDISE_ENGLISHTAK Рік тому
Nice Explanation! how about popTo any particular view like popToThirdView, popToSecondView etc ?
@rebeloper
@rebeloper Рік тому
You can do all of those with NavigationRouter found in my community: store.rebeloper.com/community If you're a SwiftUI developer you will love: Unveiling The 10 Commandments of SwiftUI - #4 Will Surprise You! 👇 ukposts.info/have/v-deo/a555f2uaq4J0r3k.html
@TheClampet23
@TheClampet23 Рік тому
trying to present a sheet from another sheet with this method, but the app dismisses the first sheet before presenting the new one. how would you solve for that?
@rebeloper
@rebeloper Рік тому
A sheets should be presented upon the change of a Published variable. Use different ones for more sheets. If you need more help consider booking a mentoring session: rebeloper.com/mentoring Next Learn About One Fatal Programming Mistake: Stop Making it a Hobby! 👇 ukposts.info/have/v-deo/hIWkrZh9f3llpKM.html&ab_channel=Rebeloper-RebelDeveloper
@hosseindastan6221
@hosseindastan6221 Рік тому
what about passing ID for specific view?
@rebeloper
@rebeloper Рік тому
Why would you do that? Let me show you a Progress Button in SwiftUI 👇 ukposts.info/have/v-deo/o3ZphaFpb2N3p4k.html
@BabuLal-bf6oq
@BabuLal-bf6oq Рік тому
It's really nice explanation but can you please let me know how to pass data to another view using this coordinator pattern.
@rebeloper
@rebeloper Рік тому
Check out NavigationRouter from kindaSwiftUI: github.com/rebeloperAlpar/kindaSwiftUI/wiki/NavigationRouter Next Unlock the Power of Core Data Versioning! 👇 ukposts.info/have/v-deo/inSlgI2OpqlkxHU.html&ab_channel=Rebeloper-RebelDeveloper
@russellwarwick
@russellwarwick 4 місяці тому
Nice example! However the view still knows about its navigation path. Coordinator should also address tight coupling of view and navigation. Any thoughts on this?
@rebeloper
@rebeloper 4 місяці тому
I have a solution for that (it's paid): ukposts.info/have/v-deo/pqKlpmdroIWbkXk.htmlsi=xUPBOBdeVox-C7FO Next Discover the 5 Best View Extensions in SwiftUI 👇 ukposts.info/have/v-deo/pmlyoaKjaqKKmn0.html&ab_channel=Rebeloper-RebelDeveloper
@russellwarwick
@russellwarwick 4 місяці тому
Nice concept with NStack however it doesn’t really solve the problem of the View knowing about its navigation path. The View should be dumb so it can be reused elsewhere without attaching all of that navigation logic. The view should only know that a button was pressed or an action was performed. It’s then the responsibility of a Coordinator to dictate the next step, not the view. I’m not sure if it’s a limitation with SwiftUI though.
@rebeloper
@rebeloper 3 місяці тому
Good points. I might add this into NStack at some point in the future. Next find out the Top 24 Xcode Shortcuts For Beginners 👇 ukposts.info/have/v-deo/boSmf6WrhJqpqYU.html&ab_channel=Rebeloper-RebelDeveloper
@book6202
@book6202 Рік тому
Apple should provide such classes in early version of iOS atleast 14.0 but it's available on iOS 16.0. Most of product base iOS versions supports 13.0.
@rebeloper
@rebeloper Рік тому
Totally agree. Did you wonder if will AI replace programmers? Check out my take on it 👇 ukposts.info/have/v-deo/cX6deXl5fn9esIE.html&ab_channel=Rebeloper-RebelDeveloper
@iansmith9368
@iansmith9368 8 місяців тому
I have tried this pattern using NavigationLinks instead of buttons, but the environment doesn't get passed 'down'. Perhaps NavigationLink doesn't create a child.
@rebeloper
@rebeloper 8 місяців тому
Maybe you're not using the latest SwiftUI. Check out a solution for all SwiftUI versions here: ukposts.info/have/v-deo/pqKlpmdroIWbkXk.html&ab_channel=Rebeloper-RebelDeveloper BTW have you seen Firebase Login in SwiftUI: Step by Step Masterclass 👇 ukposts.info/have/v-deo/cZyDi5esbGedqKs.html&ab_channel=Rebeloper-RebelDeveloper
@sergioudalov361
@sergioudalov361 Рік тому
looks nice, but what if we got 100 screens?) it will be very huge enum...)
@rebeloper
@rebeloper Рік тому
Still clearer than having those 100 screens scattered all of=ver the project, right? Let me show you a Progress Button in SwiftUI 👇 ukposts.info/have/v-deo/o3ZphaFpb2N3p4k.html
@WuatDaFack
@WuatDaFack 6 місяців тому
every time I push a page and then I use the back button my app crashes, anybody knows why?
@rebeloper
@rebeloper 6 місяців тому
I would need to take a look at your full code. Book a session here: rebeloper.com/mentoring 🚨 A Subscriber Sent Me a Junior Swift Interview Challenge, Let's Solve It 👇 ukposts.info/have/v-deo/q6Whrnefh591u2w.html&ab_channel=Rebeloper-RebelDeveloper
@TharinduRameshKetipearachchi
@TharinduRameshKetipearachchi Рік тому
I think this isn't thread safe @Published var path = NavigationPath() Xcode giving me the following error Publishing changes from within view updates is not allowed, this will cause undefined behavior.
@rebeloper
@rebeloper Рік тому
Put @MainActor on your class :) Here's Why You Shouldn't Nest Your Code 👇 ukposts.info/have/v-deo/fZOWhZyPaZthzok.html&ab_channel=Rebeloper-RebelDeveloper
@TharinduRameshKetipearachchi
@TharinduRameshKetipearachchi Рік тому
@@rebeloper Thanks ☺
@TharinduRameshKetipearachchi
@TharinduRameshKetipearachchi Рік тому
@@rebeloper Still I'm getting that warning Publishing changes from within view updates is not allowed, this will cause undefined behavior. This is coming for following line @Published var path = NavigationPath()
@rebeloper
@rebeloper Рік тому
I can take a look at your code on a mentoring session and debug it: rebeloper.com/mentoring Next check out WWDC23: You're NOT Ready for this WWDC23 Wish List! 👇 ukposts.info/have/v-deo/paObn2x6gaaUumw.html&ab_channel=Rebeloper-RebelDeveloper
@nicolaiharbo2201
@nicolaiharbo2201 7 місяців тому
This wouldn't work, if you want to present a sheet from a detail view. Then the coordinator will present that sheet from the initial view, leading to a warning in the console
@rebeloper
@rebeloper 7 місяців тому
Thanks for letting me know. Since than I have perfected a library for SwiftUI Navigation: store.rebeloper.com/nstack BTW watch me take my own SwiftUI test here 👇 ukposts.info/have/v-deo/cYCJhmugj45emJ8.html&ab_channel=Rebeloper-RebelDeveloper
@redhotbits
@redhotbits 8 місяців тому
why?
@rebeloper
@rebeloper 8 місяців тому
For me it makes my code cleaner. Stop using Spacer in SwiftUI (and what to use instead) 👇 ukposts.info/have/v-deo/i2aTqZyFoX-lxoU.html&ab_channel=Rebeloper-RebelDeveloper
@StianF
@StianF 2 місяці тому
What about actual use cases where data needs to be passed back and forth, or where a single view can be presented as both page and sheet and cover? We gotta pay $2000 dollars to learn? Bullshit moneyscam
@rebeloper
@rebeloper Місяць тому
Use an EnvironmentObject to access data from any of the views. Next escape the GeometryReader nightmare 👇 ukposts.info/have/v-deo/spZehaSva6KStWg.html&ab_channel=Rebeloper-RebelDeveloper
@AryamanSharda
@AryamanSharda 29 днів тому
If this screen is reused elsewhere in the app, in a different user flow for example, then specifying the Coordinator in the View makes it so coupled that I wouldn't be able to reuse this view anymore right? I wouldn't be able to use it with both CoordinatorA and CoordinatorB. The equivalent `Page` enum for CoordinatorA and CoordinatorB may have entirely different cases so specifying a single Coordinator and making calls like `coordinator.push(.banana)` make it so I can't reuse this view in any other flow in my app.
@rebeloper
@rebeloper 10 днів тому
Check out this new version and let me know what you think: store.rebeloper.com/navigation-coordinator Next check out my latest video about Mastering SwiftUI Error Alerts: Fix Your App in Seconds! 👇 ukposts.info/have/v-deo/n5yYZaGmhImqrXU.html&ab_channel=Rebeloper-RebelDeveloper
Dependency Injection in SwiftUI, the Best Pattern
14:55
Rebeloper - Rebel Developer
Переглядів 3,5 тис.
🔥 Україна виходить у ФІНАЛ ЄВРОБАЧЕННЯ-2024! Реакція alyona alyona та Jerry Heil #eurovision2024
00:10
Євробачення Україна | Eurovision Ukraine official
Переглядів 175 тис.
Дурнєв дивиться сторіс ZОМБІ #47
53:48
Aleksey Durnev
Переглядів 686 тис.
Эта Мама Испортила Гендер-Пати 😂
00:40
Глеб Рандалайнен
Переглядів 3,1 млн
STOP using NavigationStack in SwiftUI - Navigation Coordinator is BETTER
28:40
Rebeloper - Rebel Developer
Переглядів 1,1 тис.
How to use the Coordinator pattern in iOS
18:12
Paul Hudson
Переглядів 70 тис.
STOP Using Navigation Stack in SwiftUI! Use THIS Instead...
39:37
Rebeloper - Rebel Developer
Переглядів 9 тис.
Enum Navigation in iOS 16
14:19
Stewart Lynch
Переглядів 10 тис.
Creating a Custom Framework in Swift in 2022
26:54
Rebeloper - Rebel Developer
Переглядів 13 тис.
Advanced Coordinators in iOS
18:55
Paul Hudson
Переглядів 40 тис.
SwiftConf 2023 - Paul Hudson: Level up your SwiftUI
43:16
SwiftConf
Переглядів 8 тис.
НЕ ПОКУПАЙТЕ НОВЫЙ СМАРТФОН, ПОКА НЕ ВЫШЕЛ ЭТОТ [2024]
13:25
Thebox - о технике и гаджетах
Переглядів 78 тис.
🤏 САМЫЙ ТОНКИЙ гаджет #Apple! 🍏
0:29
Яблочный Маньяк
Переглядів 588 тис.