How to use the Coordinator pattern in iOS

  Рет қаралды 71,365

Paul Hudson

Paul Hudson

5 жыл бұрын

The coordinator pattern takes responsibility for navigation out of your view controllers and into a separate class, helping you make your view controllers smaller and simpler.
In this video I'm going to explain the problem coordinators solve, walk you through the code required to use them, then give a complete coding demo with a new Xcode project.
Coordinators were pioneered on iOS by Soroush Khanlou - he does tweet from time to time at @khanlou, but really you should check out his blog at khanlou.com.
I've also published articles on coordinators you might find useful:
- How to use the coordinator pattern in iOS apps: www.hackingwithswift.com/arti...
- Using closures with the coordinator pattern: www.hackingwithswift.com/arti...
- How to create a custom Xcode template for coordinators: www.hackingwithswift.com/arti...

Пікірлер: 177
@mustafa521996
@mustafa521996 5 жыл бұрын
A brilliant video, such clarity! Whenever you import UIKit, Foundation is no longer required as UIKit already imports Foundation! You'd know this but others would learn from you :)
@RobertHolzapfel
@RobertHolzapfel 5 жыл бұрын
Dear Paul, thanks so much, this is one of the best material (for more advanced programmers interested in architecture, design patterns) you ever made. I am so grateful. I tried once to implement the coordinator pattern, but struggled due to the mutual dependencies. You made it completely clear to me in this video. You saved my day! Thanks god for making you a SWIFT lecturer. 👍🏻
@devbenomad2393
@devbenomad2393 5 жыл бұрын
I've been following your content on HWS for a long time now, You are the best Paul ! No competition REALLY , among the too many good iOS teachers I know you can easily stand out because of your true understanding of apple's frameworks and tools. You provide clear and COMPLETE content. I'm gonna tell all my friends about and tell them to subscribe ;)
@twostraws
@twostraws 5 жыл бұрын
That's very kind of you - thank you for the generous words!
@kastor8274
@kastor8274 5 жыл бұрын
This is the 1st video of you I watched... AWESOME ! Thanks a lot
@reymundopanaguiton6320
@reymundopanaguiton6320 5 жыл бұрын
Wonderfully done. This solves a lot of horrendous amount of task just to navigate from one scene to another. Absolutely Brilliant... I love this.
@keshavkumar1522
@keshavkumar1522 2 жыл бұрын
Thank You So Much, Paul. 🤝👏👏👌 It's pretty clear and precise. Finally I Understood this pattern.
@prakashinani708
@prakashinani708 4 жыл бұрын
Great Video Paul. Explained in easy way. I am new to iOS and looking forward to your live app session.
@reece5863
@reece5863 Жыл бұрын
Great tutorial. Thanks for everything! Subscribed.
@srinathshah7461
@srinathshah7461 5 жыл бұрын
A good overview for the Coordinator Design Pattern! Thumbs up!
@gabrielsscavalcante
@gabrielsscavalcante 5 жыл бұрын
Thank you for this video! You just made it clear to me! Congrats!
@twostraws
@twostraws 5 жыл бұрын
I'm glad to hear it! Hopefully I'll add a follow-up video soon.
@kpapagno
@kpapagno 4 жыл бұрын
I'm just starting out with iOS programming, and of course my first try is with XCode 11. Following video/text gives no compile runtime errors, but when you tap on on of the buttons, it enters the IBfunction but never executes the controller method. After some searching, it appears that you need to do something a bit different now. Instead of making code changes in AppDelegate, I had to put it in SceneDelegate on the scene function. my SceneDelagte now looks like this, and it is working! Thanks for a great tutorial and explanation! import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { var coordinator: MainCoordinator! var window: UIWindow? func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let appWindow = UIWindow(frame: windowScene.coordinateSpace.bounds) appWindow.windowScene = windowScene let navController = UINavigationController() coordinator = MainCoordinator(navigationController: navController) coordinator.start() appWindow.rootViewController = navController appWindow.makeKeyAndVisible() window = appWindow } . . .
@vinothvino1275
@vinothvino1275 4 жыл бұрын
Thanks man! It's working now.
@user-pi9rd2zr7v
@user-pi9rd2zr7v 4 жыл бұрын
Thanks
@GuruprasadM
@GuruprasadM 4 жыл бұрын
Thank you! This was very helpful.. Paul Hudson, you could consider adding the above in your website so that people with Xcode 11 can follow your tutorial better!
@jhinoventura
@jhinoventura 3 жыл бұрын
Man thx so much. I'm also just starting out and it didin't work following the tutorial. Your changes helped me now it's working. God bless you
@waxelite147
@waxelite147 2 жыл бұрын
Thank you so much, the way Paul wrote it in the "Sundays" ebook did not work even if he updated the code for the SceneDelegate, your way is working perfect. Hope Paul will update the book including your code
@milosmokic5536
@milosmokic5536 5 жыл бұрын
Thank you a lot for this. It was a pleasure to watch.
@twostraws
@twostraws 5 жыл бұрын
I'm glad you enjoyed it! 👍
@Erik123211
@Erik123211 4 жыл бұрын
Single storyboard file causes a lot of trouble when working in team. I would also create separate storyboard file for each view controller with the same name. It will help you with merge requests with less conflicts. Anyway, nice and clear tutorial 👌
@fgary
@fgary 5 жыл бұрын
An awesome explainer on coordinators! Thanks Paul
@twostraws
@twostraws 5 жыл бұрын
Thank you!
@jackdanyal4329
@jackdanyal4329 4 жыл бұрын
Great tutorial!
@MrDeep0
@MrDeep0 2 жыл бұрын
Thank you for that!
@adincebic2992
@adincebic2992 5 жыл бұрын
I like that you are narrating most of the things you do in the video.
@twostraws
@twostraws 5 жыл бұрын
Yes, that's certainly my intent.
@melloguitar
@melloguitar 5 жыл бұрын
Great class about Coordinators! Thanks!
@twostraws
@twostraws 5 жыл бұрын
I'm glad you enjoyed it - make sure you watch the advanced coordinators video, which goes over lots of extras!
@multitudes389
@multitudes389 2 жыл бұрын
awesome! It took me a while to get here but finally I got it.
@jeraldo4571
@jeraldo4571 4 жыл бұрын
Such a good explanation. How about navigating back or popping view controllers and handling view controllers that are not pushed but presented?
@alonnasi7104
@alonnasi7104 Жыл бұрын
Hi Paul! thank you so much for all the hard working you and your team are doing for us all those years! I'm an iOS developer and came across you tutorial here, and just wanted to NOTE something: When using Xcode 14.2, I managed to get this pattern design to work ONLY when implementing the code at 12:00min ('AppDelegate'->'DidFinishLaunching') at ('SceneDelegate'->'WillConnectTo' method) Here's my version of it: guard let windowScene = (scene as? UIWindowScene) else { return } let navigationController = UINavigationController() mainCoordinator = MainCoordinator(navigationController: navigationController) mainCoordinator?.configRootVC() window = UIWindow(windowScene: windowScene) window?.rootViewController = navigationController window?.makeKeyAndVisible() So, to anyone having trouble implementing this tutorial at 2023 - ENJOY! Thx again 😎
@chtozaaliya
@chtozaaliya Жыл бұрын
thank you so much it really helped!
@ikajava
@ikajava 5 жыл бұрын
@Pual Hudson Such a perfect guide as always! But, how DRY is a code? You need to write all the methods inside MainCoordinator and repeat the same code in every method. Is there a way to avoid it?
@antonsleptsov3185
@antonsleptsov3185 4 жыл бұрын
Nice explanation of the concept. I have one comment regarding components coupling. Yes, applying coordinator you decouple one VC from the other, but you introduce VC+Coordinator coupling instead. As one of possible solution to resolve it we may use blocks like onButtonPressed: () -> Void , which we can inject into VC and do implementation of the block right inside of coordinator. What do you think? Found the answer in the end of next video...
@user-dl7uw3dy9c
@user-dl7uw3dy9c 2 жыл бұрын
great! big thx
@hectorsvill
@hectorsvill 4 жыл бұрын
Thank You!
@ThomasDaleWood
@ThomasDaleWood 5 жыл бұрын
Great video Paul! It would be great to have a video on how to implement/when to use Child Coordinators. Right now when I do Coordinators I only have one. An example showing how to make a destroy children, and also showing how to use Coordinators to create different views on iPhone vs iPad would be super helpful!
@twostraws
@twostraws 5 жыл бұрын
I'm working on it!
@charlesphelps9804
@charlesphelps9804 4 жыл бұрын
If you are using Xcode 11 or newer and want to use the new SceneDelegate, you need to move the AppDelegate code to the SceneDelegate.scene method. After the guard clause, you create the navController, the coordinator, and set the rootViewController. I don't think you need the code to initialize the window because that's taken care of. guard let _ = (scene as? UIWindowScene) else { return } let navController = UINavigationController() // Don't forget to add var coordinator: MainCoordinator? to the top of the class coordinator = MainCoordinator(navigationController: navController) coordinator?.start() window?.rootViewController = navController window?.makeKeyAndVisible()
@tetracosanoic
@tetracosanoic 3 жыл бұрын
thank you so much, couldn't figure out what was wrong with my code
@vladpodvoiskyi2307
@vladpodvoiskyi2307 2 жыл бұрын
@@tetracosanoic the same situation
@five-am
@five-am 5 жыл бұрын
Simple, Great.
@002sanjay
@002sanjay 4 жыл бұрын
Amazing Clarity of Concept. Thanks Paul. 1 question here, navController in MainCoordinator is visible to all viewcontrollers ?
@5fcgdaeb
@5fcgdaeb 3 жыл бұрын
Nice and clear explanation! I am guessing the view controllers should hold a reference to the Coordinator protocol but not the concrete MainCoordinator class?
@SudhanshuSrivastavaIndia
@SudhanshuSrivastavaIndia 5 жыл бұрын
wow, this is awesome
@ritu-pnwhiker8419
@ritu-pnwhiker8419 4 жыл бұрын
Could you please suggest me, how to build custom navigation bar with left and right bar items/buttons using coordinators? For eg. Once user does login and navigates to home screen, that is logout button as right navigation bar item. This will display in all the screens after login screen. How to handle such thing? That all logic goes somewhere in coordinator?
@chrispy104k
@chrispy104k 5 жыл бұрын
This is awesome. So much to digest. Would be nice to be able to get the source code.
@MohammedJoKu
@MohammedJoKu 4 жыл бұрын
What a brilliant video and I followed the videos instructions there was only one thing that doesn't work when I initialize each ViewController's Coordinator as a weak var functions of the coordinator doesn't get called and when I just remove the weak keyword from coordinator's initialization everything works great idk why and how and this happens in Xcode 11.5
@rebeloper
@rebeloper 5 жыл бұрын
Amazing!
@twostraws
@twostraws 5 жыл бұрын
Thank you!
@joshadams8761
@joshadams8761 5 жыл бұрын
Helpful video. Thanks for making it. If you ever tire of Swift, you could work as an animator at Pixar. In that case, please make a WALL-E prequel. I would like to see how the apocalypse happened and how WALL-E experienced it.
@stanymiles
@stanymiles 5 жыл бұрын
This is the whole chapter of your Patterns book)) I use this pattern now. Very useful. (btw I have 8 of your books)
@twostraws
@twostraws 5 жыл бұрын
I don't actually include coordinators in Swift Design Patterns, because it's a bit too much of a UI pattern rather than a general language pattern. I did try, but I just don't think it fitted well. Thank you for your support!
@stanymiles
@stanymiles 5 жыл бұрын
@@twostraws I checked right now, and you're true. It might be some video then. But I'm pretty sure that I learned this pattern from your material. The code snippet I have is exactly the same. (So much stuff to learn, so I don't even remember where I've learned it from)))
@twostraws
@twostraws 5 жыл бұрын
@@stanymiles This is the video version of an article I wrote, which probably explains it: www.hackingwithswift.com/articles/71/how-to-use-the-coordinator-pattern-in-ios-apps
@sebastianstokkebro6481
@sebastianstokkebro6481 5 жыл бұрын
Would it be possible to use this practice on programmatically created views/viewcontrollers? Say a dynamic pop-up view.. how does the coordinater handle constraints on dynamic views? how and where should the constraints be handled? Great video btw :-)
@dev_jeongdaeri
@dev_jeongdaeri 3 жыл бұрын
Super awesome!!! 😭
@NikaKirkitadze
@NikaKirkitadze 4 жыл бұрын
Great video, what about coordinators with multiple Storyboard?
@ayon3527
@ayon3527 5 жыл бұрын
looks good Mr. Hudson
@twostraws
@twostraws 5 жыл бұрын
I'm glad it's useful!
@nebojsapavlovic2231
@nebojsapavlovic2231 5 жыл бұрын
You are great! Can you show coordinator for those who don't use storyboards?
@ahopdanzer
@ahopdanzer 3 жыл бұрын
nice video to explain mvvmc
@abhishekkumarthakur2116
@abhishekkumarthakur2116 4 жыл бұрын
Awesome video. But how to handle a custom transition presentation of a view controller.
@smongo1234
@smongo1234 5 жыл бұрын
I can recommend coordinators. Really nice video explaining it Paul, as always. I am wondering though, why would the Coordinator protocol describe having a UINavigationController? It should be up to the specific coordinators wether or not it presents things in a navigationcobtroller or something else like a tabbarcontroller or even something custom. The viewcontrollers also don’t need to know that, but with the protocol, they do. Am I missing something?
@twostraws
@twostraws 5 жыл бұрын
Yes, I think that's a fair comment - it's usually a better idea to expose as little as possible.
@khongduocbocuoc
@khongduocbocuoc 5 жыл бұрын
What if I want to present another NavigationController instead of push
@atrcl95
@atrcl95 5 жыл бұрын
can you show more examples on where the child coordinators can be used, also an app where we have multiple coordinators?
@mikapps9349
@mikapps9349 5 жыл бұрын
Agree 100% ...
@twostraws
@twostraws 5 жыл бұрын
I'm on the case!
@mikapps9349
@mikapps9349 5 жыл бұрын
Christmas is coming early! Thanks Paul.. Could you please consider covering - dealing with default back button (ie replacing with custom), avoiding duplicate child coord entries (its an array so is possible - I had an app the if you pressed 2 buttons at once it would as 2 flow A and flow B... rare case but I did have to prevent this from happening. Wonder if childCoord could be a set? Looking forward to this release!!
@rafaelrincon3109
@rafaelrincon3109 5 жыл бұрын
Looks like he did just that: kzfaq.info/get/bejne/q8tyrMVhsqixgWg.html
@GuruprasadM
@GuruprasadM 4 жыл бұрын
​@@twostraws Great tutorial overall, but it doesn't work entirely for Xcode 11.. Ken Papagno has commented below as to what changes are needed.. could you update those in your website under a new section called Xcode 11 maybe so that they don't face issues?
@deepesh259nitk
@deepesh259nitk 5 ай бұрын
Dear Paul. Can you recommend any links on how would the co-ordinator pattern be applicable to Swift UI?
@DigitalHole
@DigitalHole 5 жыл бұрын
Awesome.
@twostraws
@twostraws 5 жыл бұрын
Thank you!
@johnsnow1749
@johnsnow1749 5 жыл бұрын
Where can I download the tutorial project?
@vandanpatel3395
@vandanpatel3395 5 жыл бұрын
GOLD!
@twostraws
@twostraws 5 жыл бұрын
I'm glad you liked it!
@user-il5ur7kj3i
@user-il5ur7kj3i 4 жыл бұрын
Thanks for information. I just do not understand one thing, if we are using coordinators to break coupling than why do we couple each viewcontroller to exactly one coordinator? Does not this coupling breaks whole idea of coordinators, and making viewcontroller not reusable?
@PedroNeto-nk3td
@PedroNeto-nk3td 3 жыл бұрын
not really, if u have some view controllers like A -> B -> C -> D, but now if u need to add a "X" view between B and C, you would need to go on both to change the flow to A -> B -> X -> C -> D (very simple example) which on a large app might cause problems you won't see at first But if you're using coordinators u just have A,B,C,D,X... and change what points to what in the coordinator.... remember in the example the view controller func buyTapped, the "coordinator?.buySubscription" can be anything. And even if you change the flow of the app, you won't need to change this file (ViewController or BuyViewController), only the coordinator, cause the viewController doesn't know what view coordinator?.buySubscription is...
@fordee1964
@fordee1964 5 жыл бұрын
I still think navigation is pretty easy the normal way. The main problem for me is deciding on a navigation stack or going modal. I think with a project greater than around 15,000 lines of code, I would look at a coordinator pattern. In other words if it's a simple app, I would go with the normal way of doing things.
@greglhotellier5612
@greglhotellier5612 4 жыл бұрын
Someone just told me I might be somewhere here... Thanks for the shoutout! And I hope this joke will have a long life! 😁
@sashamakedonskii
@sashamakedonskii 5 жыл бұрын
Thanks for sharing. Do you use Viper pattern ? It will be great to see implementation of app with Viper.
@twostraws
@twostraws 5 жыл бұрын
I'm glad it was useful! No, I don't use Viper much - MVC and MVVM mostly.
@user-xv2bb6ev7b
@user-xv2bb6ev7b 4 жыл бұрын
looks like I'm missing the childCoordinators variable being initialized. the project build fails since the initialization of the MainCoordinator is not full.
@shivangvyas9584
@shivangvyas9584 4 жыл бұрын
hi @paul, i want to know how coordinator works if we want to add subview to current view controller ? subview can be like child view controller or view with date picker...can be anything...
@shivangvyas9584
@shivangvyas9584 4 жыл бұрын
whose responsibility to add subview to parentview , will it be a coordinator's responsibility or that viewcontroller's responsibility ?
@ramachandrapradhan135
@ramachandrapradhan135 4 жыл бұрын
Rama and shyam are playing football.is it simple or compound sentence,? Sr plz explain it
@Kukukuk12
@Kukukuk12 5 жыл бұрын
Is there any advantage in using storyboards for isolated view controllers over Xibs for each one? Great video as always, Paul!
@mikapps9349
@mikapps9349 5 жыл бұрын
Code only > SB > Xibs
@twostraws
@twostraws 5 жыл бұрын
Storyboards give a few bonus features that you don't get elsewhere, such as being able to design specific table view cells rather than using prototypes.
@daisoreanulaurentiu3943
@daisoreanulaurentiu3943 4 жыл бұрын
Would be nice to see a parallel with SiftUI
@clomasss3359
@clomasss3359 4 жыл бұрын
Is anyone having an issue where the frame size of UIWindow is too small for any device? I've tried two different projects.
@RobertHolzapfel
@RobertHolzapfel 4 жыл бұрын
I am thinking: If all the 'Coordinator' is actually doing, is navigation. Why wouldn't we not use just an instance of UINavigationController as "Coordinator"? Even more, when every instance of UIViewController needs a 'var coordinator : Coordinator' while the property 'var navigationController' is already implemented by UIKit for free in every VC. The creation of new VC, as well as the navigation stuff would also be "outsourced' to the, well lets say, to the "MainCoordinatorNC" that inherits from UINavigationController. Bad idea?
@cnstntVAL
@cnstntVAL 4 жыл бұрын
Confused as to why this isn't working for me, i've copied the code bit by bit and whenever i click on the buttons nothing seems to happen. Strange
@loverock5577
@loverock5577 5 жыл бұрын
I love this video. Thanks a lot. But can you show how to use coordinators with multiple storyboard?
@jaydice1
@jaydice1 4 жыл бұрын
You should put each storyboard in their own file so that other users on git can work on other classes while you work on your classes.
@TheBishopOfBarton
@TheBishopOfBarton 5 жыл бұрын
I am trying to replicate this, but when I run it, I get a build fail "Type ViewController does not conform to protocol "Storyboarded" in ViewController.swift. I thought this was effectively disabled through removing Main. I think I have followed all your steps... can you help? Thank you.
@lukacefarin2221
@lukacefarin2221 4 жыл бұрын
Check that method name in Storyboarded protocol is the same as method name in Storyboarded extension.
@amber.k
@amber.k 4 жыл бұрын
I have one doubt here: UIViewController object have storyboard property initialized. Will it increase memory if we create new main storyboard instance everytime?
@ruyneto2007
@ruyneto2007 3 жыл бұрын
Where did you saw?
@williamsquires3070
@williamsquires3070 5 жыл бұрын
I get a compile-time error: “Method ‘instantiate()’ in non-final class ‘ViewController’ must return ‘Self’ to conform to protocol ‘Storyboarded’. I checked, and the static func instantiate() does return Self, namely: return storyboard.instantiateViewController(withIdentifier: id) as! Self Is it because my Xcode only has Swift 4.0? The error points to the line: static func instantiate() -> Self {
@twostraws
@twostraws 5 жыл бұрын
I'm not able to debug small snippets of code, I'm afraid. I definitely suggest that you start by updating Xcode to rule out any problems, but once you've done that and compared your code to mine on screen, you're welcome to email me if you still have problems.
@TheVirusRko
@TheVirusRko 4 жыл бұрын
Paul do you think it would be a good idea to mark the coordinator in VC's as Coordinator! instead of Coordinator? I mean, anyway without the coordinator the app will have no sense.
@whitecountryoldroad
@whitecountryoldroad Жыл бұрын
Super late response, but notice that coordinator has a weak reference, which is just a pointer to an object that doesn't protect the object from being deallocated by ARC. That means that you cannot know for sure whether the coordinator variable holds a reference to the AppCoordinator when you try to access it in the VC.
@condemned77
@condemned77 5 жыл бұрын
How can you declare the coordinator in the ViewController as being 'weak'. When I do this, the compiler says'weak' must not be applied to non-class-bound 'Coordinator'; consider adding a protocol conformance that has a class bound. Did I miss something or is it because I'm using Swift 5 now.
@emrahkorkmaz3991
@emrahkorkmaz3991 5 жыл бұрын
hey, did you put to class keyword to your protocol. such as CoordinatorProtocol: class {...} ?
@saifdj
@saifdj 3 ай бұрын
Is there anyway to download the demo project? else tip: you should start providing the links to demo projects 😄
@Curpop
@Curpop Жыл бұрын
Liking this 3 years after i watched it the first time
@souravvashisht9658
@souravvashisht9658 5 жыл бұрын
What if we need to use this pattern in multiple storyboard like if I have 2 storyboard how can I use this pattern there ?🤔
@sub040
@sub040 5 жыл бұрын
same as you, maybe we should pass storyBoard name as an argument in function
@mehmetali1945
@mehmetali1945 Жыл бұрын
Where do we use the childcoordinators array
@user-kb3lc5in9d
@user-kb3lc5in9d 5 жыл бұрын
What is proper way move from Buy view controller to account view controller using coordinators? Buy view controller must be removed from stack?
@mikapps9349
@mikapps9349 5 жыл бұрын
You tell the coordinator to pop it from the stack (or remove if using modal) - but you have to create a custom back button as the standard back button pops it for you. There are some gotta's with coordinators that are not discussed in this intro video.
@kosta3ov
@kosta3ov 5 жыл бұрын
Can view controller delegate prepareForSegue in coordinator?
@twostraws
@twostraws 5 жыл бұрын
Can it? Sure - it can just pass on the call. *Should* it? No; I don't recommend using segues.
@khongduocbocuoc
@khongduocbocuoc 5 жыл бұрын
15:53 is it violating Interface segregation principle ?
@federicodominguezdaniel
@federicodominguezdaniel 4 жыл бұрын
Yes , but on 15:13 he explains why and recomend to use protocols in larger apps. (Sorry for my bad english haha )
@xVinoz
@xVinoz 11 ай бұрын
Thank god for saving me of using storyboards lol
@TheBardsCorner
@TheBardsCorner 4 жыл бұрын
How should we complete the part at min 12:00 in Xcode 11 since the introduction of the Scene Delegate file? I tried to put the code as yours but it doesn't work. I tried to put that in Scene Delegate but it doesn't work. Thanks
@joemccraw2374
@joemccraw2374 4 жыл бұрын
Take a look at this comment above from Łukasz Gierałtowski "If you are using Xcode 11 or newer: 1) Coordinators project > General > Deployment Info > Target > set 12.1 2) AppDelegate.swift > remove both UISceneSession methods, 3) Info.plist > remove Application Scene Manifest property."
@002sanjay
@002sanjay 4 жыл бұрын
@@joemccraw2374 You can add the below code to your scenedelegate will connect to session class window = UIWindow(frame:windowScene.coordinateSpace.bounds) window?.windowScene = windowScene let navController = UINavigationController() mainCoordinator = MainCoordinator(navController: navController) mainCoordinator?.start() window?.rootViewController = navController window?.makeKeyAndVisible()
@Charlot1914
@Charlot1914 2 жыл бұрын
I don't get it why we need to set vc.coordinator = self. And what's the point of the childCoordinators?
@markuspfeifer8473
@markuspfeifer8473 4 жыл бұрын
I came up with a solution inspired by functional programming. My view controllers manage presenting the next view controller on their own, but which view controller they present is decided by a closure. All they know about is what data they can provide to the next view controller and possibly some labels for the data. For example, if you have a view controller that allows you to select three numbers using sliders and submits these numbers with an ok button, that one would take a closure of type (Float, Float, Float) -> UIViewController and some description what those sliders mean etc
@markuspfeifer8473
@markuspfeifer8473 4 жыл бұрын
If you have some chain of views, this essentially amounts to currying. A view controller is just a higher order function.
@markuspfeifer8473
@markuspfeifer8473 4 жыл бұрын
Both solutions are mostly equivalent, but they have pros and cons. Coordinators introduce a clearer separation between logic and view, but they are more verbose and one has to remember assigning the coordinator to each view controller. My solution gives you a clear idea what data a view can provide and consume and you can see entire workflows ahhh a glance, but I actually tend to declare them in root view controllers of storyboards (which do nothing but coordinating those Workflows and maybe hold some references that they pass down). It’s a bit of a technical artifact that coordinators in my solution happen to be view controllers. Also, your solution makes it easy to instantiate the exact same view controller from everywhere by calling some function which is nice and avoids a boilerplate in my solution that I have to tell every view controller that wants x where to find x; on the other hand, that kind of looks like tight coupling through the backdoor as you replace knowing which view controller to instantiate by knowing which coordinator function to call.
@MuhammadAli-zv5vz
@MuhammadAli-zv5vz 5 жыл бұрын
Great video.How to use this coordinator pattern for separate the delegates and datasources
@twostraws
@twostraws 5 жыл бұрын
They are different issues. If you watch my Swift on Sundays videos you'll see I talk about pulling out data sources quite a lot.
@MuhammadAli-zv5vz
@MuhammadAli-zv5vz 5 жыл бұрын
Paul Hudson oky
@vadirajhippargi9285
@vadirajhippargi9285 5 жыл бұрын
Just wondering what if we also wanted to pass data between VCs. With Coordinator pattern, should coordinator code be made to take care of that too. How do we let VC know the coordinator telling "If you want me to push this to so and so VC, then you are also supposed to supply me with this and this data".
@vadirajhippargi9285
@vadirajhippargi9285 5 жыл бұрын
@David van Enckevort Thanks David, So Essentially that code also need to go into Coordinator, right?
@vadirajhippargi9285
@vadirajhippargi9285 5 жыл бұрын
@David van Enckevort Thanks, this helps.
@oboomabom
@oboomabom 5 жыл бұрын
How would you use this with view controllers that need information to know what to display? E.g. a profile would need an ID. Just as parameters to the coordinator functions?
@mikapps9349
@mikapps9349 5 жыл бұрын
You inject the data into the VC when you init them within the coordinator.
@oboomabom
@oboomabom 5 жыл бұрын
@@mikapps9349 how does the coordinator get it though?
@mikapps9349
@mikapps9349 5 жыл бұрын
@@oboomabom when you create the coordinator you give it all the data in needs so it can pass what is required to VCFoo or VCBar etc...
@oboomabom
@oboomabom 5 жыл бұрын
@@mikapps9349 I'm a bit confused. If it needs the data when it's created then isn't that, in a way, a soft dependency with the destination view controller? Like how would I use a coordinator to go to the profile of one of my users that I'm displaying in a table view on my current vc?
@mikapps9349
@mikapps9349 5 жыл бұрын
Say you have a UsersCoordinator, which controls showing / hiding your views. One view (View Bar) could be a list of all students, another view (View Foo) could be a single profile of one user. Note view bar and foo don't know about each other at all! Flow example, you want to show View Bar.... the coordinator's role is to show this. You may have a UserModelController class (that's injected into the coordinator) which you can ask for allUsers or a singleUser. Coordinator shows View Bar, and provides the VC with all necessary info. You then select a cell to show a users profile. You ask the coordinator - showFooView(withUserID:) and pass in that users ID (for example) then within the coordinator you can ask UserModelController ... get this user with this ID and then inject that into the FooView. All flow is directed through the coordinator. This is how I do it, maybe there are other ways. With protocols etc, but this simple. Hope that helps. Otherwise create a question on stackoverflow.
@WilliamGattoFilho
@WilliamGattoFilho Жыл бұрын
Dude you rock, Imma pay ya a coffee in person one day. 5:13 7:39
@petersuvara
@petersuvara 4 жыл бұрын
MVC and Apples setups are pretty easy to understand and work well. Coordinators can easily become over engineered, and everyone creates an app that uses a different coordinator implementation, which you'll have to learn EVERY TIME! Yes, work in the industry long enough and you'll see that every app has a different Coordinator implementation that's just as different as you could imagine, without the supporting documentation (like you have with apples standards library).
@allesmaze8710
@allesmaze8710 4 жыл бұрын
do you have an instagram or other kind of social media page?
@undead-pixl
@undead-pixl 5 жыл бұрын
This is so awesome and i can see the benefits...but for some reason i am still not getting it 100% :/ And I don't want to copy/paste the code and just use it without understanding how it is made from skratch. I guess i will put this video on repeat and try to figure out which pieces i am missing :) Great video!
@twostraws
@twostraws 5 жыл бұрын
Feel free to tweet me (@twostraws) if you have specific questions. I hope to make a follow-up video addressing common questions, so please ask!
@undead-pixl
@undead-pixl 5 жыл бұрын
@@twostraws Tnx Paul! I will do that if i hit a wall again. You explained it very well so i expect going over the video and getting some reading done will clear things up :)
@undead-pixl
@undead-pixl 5 жыл бұрын
@@twostraws HA! I actually did it! It feels so much clearer now. I guess this is what you feel every day....kinda :) I feel i have the power to manipulate the world now! I acutally went over your article and it helped going over code line by line with your added comments there. Awesome stuff!
@AivarsMeijers
@AivarsMeijers 5 жыл бұрын
Great video, subscribed. Curious how wasn’t subscribed yet ¯\_(ツ)_/¯ Already used coordinators for one of small projects after reading your post “How to use the coordinator pattern in iOS apps”. Now I’m thinking about slow migration to MVS+Cordinators for one of the projects with really huge VC classes. Could be interesting to hear opinion and expierience from others about risks and edge cases for slow migration. No way to rewrite everything in one release, there are more than 40 screens in that app.
@twostraws
@twostraws 5 жыл бұрын
You probably hadn't subscribed because I don't do that much on KZfaq - I'm figuring things out slowly :) You don't need to dive in with coordinators everywhere; try adding it to just part of your app and go from there.
@JurisAndersons
@JurisAndersons 4 жыл бұрын
Paul, giving all view controllers "weak var coordinator: MainCoordinator?" dependency is not a good thing...
@JurisAndersons
@JurisAndersons 4 жыл бұрын
@@MsUzoAgu several ways. 1) pass a closure instead of coordinator for callback, 2) hide MainController behind a protocol.
@petersuvara
@petersuvara 4 жыл бұрын
What? Ofcourse you can reuse Segways... and the point is that you want it hard-coded so you have a clear flow. Sounds like coordinators are just another adventure in over-engineering. I've been using Coordinators in this pattern and if you have some interesting design, you'll run into all the same problems as and more when it comes to displaying content. In fact, it becomes more complex because the state of screens becomes lost as you move between views. I would only recommend coordinators for large enterprise applications without novel/unique design.
@user-pl3je1vt8k
@user-pl3je1vt8k 4 жыл бұрын
Start here 3:33
@kikiwora
@kikiwora 5 ай бұрын
Storyboards and XIBs are not needed. We all shall use Code and Previews
@tenminutetokyo2643
@tenminutetokyo2643 4 жыл бұрын
Nice but keep em under 10 mins.
Advanced Coordinators in iOS
18:55
Paul Hudson
Рет қаралды 41 М.
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 29 МЛН
Smart Sigma Kid #funny #sigma #memes
00:26
CRAZY GREAPA
Рет қаралды 2,7 МЛН
MVVM + Data Driven UI на практике swift
40:22
The Swift Developers
Рет қаралды 21 М.
Write Better Code with Swift Algorithms
34:34
Paul Hudson
Рет қаралды 32 М.
What's going on with Windows Laptops?
10:30
Marques Brownlee
Рет қаралды 2,6 МЛН
SwiftUI's Mini Coordinator Pattern Demystified
23:21
Rebeloper - Rebel Developer
Рет қаралды 1,5 М.
VIPER Architecture in iOS (Swift)
1:02:07
Venu Gopal Tewari
Рет қаралды 10 М.
UINavigationController [THE BEST WAY TO USE IT]
38:20
Rebeloper - Rebel Developer
Рет қаралды 8 М.
Как удвоить напряжение? #электроника #умножитель
1:00
Hi Dev! – Электроника
Рет қаралды 1,1 МЛН
Лучший браузер!
0:27
Honey Montana
Рет қаралды 1,1 МЛН
ноутбуки от 7.900 в тг laptopshoptop
0:14
Ноутбуковая лавка
Рет қаралды 3,5 МЛН