UIViewController - Lifecycle | Swift | viewDidLoad

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're talking about the UI view controller lifecycle methods you know viewdidload viewwillappear view did disappear all that stuff i'm gonna talk about when each one is called you know what you should be doing in each one and i'm also gonna share a common example of the difference between view to load and view will appear because that's a very common situation new developers get themselves into and we're also gonna dabble into the whole view will layout sub views and view did layout sub views but first I got to thank today's sponsor today's video is sponsored by Devon Mountain dev Mountain is an in person coding and design bootcamp that offers housing at no extra cost for full time immersive students now if you know my story I myself and they bootcamp grad it was part of the process to help launch my iOS developer career which I'm going on year five now in this wonderful profession but aside from their iOS development program dev Mountain also offers programs in web development software QA and UX design they even have a career services team to help you with job placement and financing options are available and dev Mountain loves hearing from my viewers so if you or someone you know is ready to start this journey into iOS development be sure to check out the link in the description alright back to the video before we dive into the details of each one let's talk about when these methods are called because I think that's a source of confusion for people just starting out right you may see a method and then you're expecting a very explicit method call to actually call that method well these ones get called automatically by the system so you're not going to see you know viewdidload get explicitly called now when they're called that's what we're gonna dive into with the specifics of each one but just know they're called automatically now when you create your own view controllers your sub classing a UI view controller and when you subclass the UI view controller you're inheriting all these view lifecycle methods so when you're using viewdidload in your view controller that's why you're calling super dot viewdidload because you want to get all the magic that Apple built on the view controller and then the code you enter in viewdidload is your own custom code for the specific subclass of view controller now I just use viewdidload as the example but that applies for all these lifecycle methods so if you're wondering why you're calling super in these that's why first up viewdidload now if you did load gets called when the view controllers content view gets created in memory or loaded from the storyboard what's the content view let's pull up a storyboard to show you this real quick so here on the we have a view hierarchy of a view controller if you've ever noticed when you create a view controller on storyboard you get this view right here by default this is the view controllers content view so when that view first gets created in memory viewdidload gets called this has nothing to do with what's displaying on the screen this is when it gets created in memory or loaded from the storyboard now because it's loaded from the storyboard if I go to a screen here that is why all your outlets will have a guaranteed value in viewdidload right that's why your outlets can be implicitly unwrapped optionals you may have wondered like why am i forcing wrapping these outlets well again in viewdidload your outlets are guaranteed to have a value that's why we can do this now let's move on to viewwillappear and then we'll do the example on the difference between view to load and view will appear now view will appear it gets called just before the content view is added to the actual apps view hierarchy so just before it actually shows up on the screen so let me talk about the word up here real quick because being added to the apps view hierarchy means it's in the view hierarchy however it may not appear on your screen and that could be for a reason maybe your view is you know my view dot hidden but it's still in the view hierarchy or maybe there's another view on top of that view so just because it's not actually showing up on the screen view will appear still happens because it's actually in the view hierarchy so wanted to clear up that confusion you know viewwillappear doesn't necessarily mean it's actually showing on the screen again because you could have dot is hidden or view could be on top of it but it's still there now let's get into a very common example on when to use you to load and viewwillappear it this happens all the time in apps so let me pull up the simulator here so here in this app here so this search vc viewdidload is called BAM this is there now let me go to the next screen this is the get up followers course by the way so now this is the search BC this is the follower list VC right now when I go back right viewdidload is not going to get called but view will appear it gets called because on a navigation controller you're just putting a new view controller onto the stack but this search VC is still there so therefore viewdidload only gets called that first time but every time I come back to the screen viewwillappear gets called now here's how we're using this in this app right so here on the search VC we're using it to reset the user name text field to blank so every time we come back to the screen we want that text field to be blank if we didn't have this line code that sa llen zero four zero zero would always still be there but now let's move setting the navigation controller to false interview did load up here so this will prove that viewdidload only gets called once right because at the very first time we run it we're gonna set that navigation bar hidden equal to true right okay look no navbar and then we go to the next screen hit get followers now there's a nav bar right we're showing the nav bar in this screen but when we go back to the the search BC we don't want to show the nav bars just a styling choice but when we go back because viewdidload only got called that first time now we have a nav bar here with this search thing right because this navigate set navigation bar hidden to true is not getting called so that's why we call this in viewwillappear right so remember that viewdidload only gets called that once however viewwillappear is going to get called every time the view comes on screen so now that we have that back in view will appear every time the view comes in on screen we're making sure we hide that navigation bar so this is just one common example of using you know view will appear over viewdidload a lot of beginning developers get caught up on that they're not sure why the behavior is happening and they don't understand that viewdidload only gets called that first time and then if you want to change stuff every time a screen you know shows up you got to do it in viewwillappear all right let's move on to view did appear so right we just did view will appear so that happens before the view appears so you can change it and then now you have you did appear so this is what gets called after the view is in the apps view hierarchy and could potentially be showing up on your screen so you want to do stuff here like maybe animations right because if you start an animation in viewdidload or viewwillappear it may start a little bit too soon right you want a visual animation to start once the screen you know it is showing up now moving on to view will disappear right so this is very similar to view will appear except instead of getting called before the view is added to the apps view hierarchy this gets called just before it's removed from the apps view hierarchy so you'd want to do stuff in here like you know committing Save Changes right so let's say you're have a form on your app and before you dismiss the form you want to make sure you save everything like even if the user dismisses you know prematurely maybe you want to save it for some reason so you would do that in view will disappear because you want to make sure you do that before the view is gone and then you have you did disappear so this is something you'd want to do after the view has been removed from the views hierarchy now let's wrap this up by talking about the less common ones and that is view will lay out sub views and view did lay out sub views now view will lay out sub views is called when your views bounds change and it's before all the sub views have been laid out very common example of the views bounds changing is when you rotate from portrait to landscape on your phone right you can see the views bounds changing kind of in real time in the animation so view will lay out sub views gets called after the views bounds change but before it relays out all the sub views that got you know on the screen and by default like view will lay out sub views and view did lay out sub views like there's no default implementation they're just there to give you the programmer access to this point in time so you can make changes if you need to so if you will lay out sub views gives you that chance you know again in the example after you rotate your device before it lays out all the sub views on the screen you have the chance to do any customization and then view did lay out sub views again gives you that chance after the sub views have been laid out if you need to do anything like maybe an animation or something like that so that was a quick overview of the view controller lifecycle methods if you enjoyed my teaching style check out the link on the screen I started creating my own courses let's see in the next video
Info
Channel: Sean Allen
Views: 24,667
Rating: undefined out of 5
Keywords: Swift, Swift Tutorial, iOS Developer, iOS Development, Swift Code Tutorial, Sean Allen, view controller life cycle ios, view controller lifecycle, view controller life cycle swift, view controller life cycle, UIViewController lifecycle, UIViewController lifecycle swift, swift view lifecycle, view life cycle in swift, swift view controller life cycle, viewdidload, viewdidappear, viewwillappear, viewwilllayoutsubviews, viewdidlayoutsubviews, viewdidload vs viewwillappear
Id: d7ZqxvbiTyg
Channel Id: undefined
Length: 7min 57sec (477 seconds)
Published: Tue Feb 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.