Learn about the life cycle methods Awake, didAppear and didDisappear. Learn how these work with page navigation, and understand why programmatic pages are discouraged.
- [Voiceover] WatchOS has a wide variety of controls you can use on a Watch Face. We'll activate and the deactivate. What are they for? Let's do some exploration of these three lifecycle methods. I'll mark everything out with some print statements. In the InterfaceController.swift file, find the awake method. The awake method runs when we initialize the interface control. It's like the view did load of watch OS. If you have any objects that need configuration or initialization before they start up and need the initialization, that goes in the awake method.
Just under the super there, type in print quote one awake. Select it, command copy to copy it and then go over to two interface controller and in two interface controller's awake, paste it, change one to two, and then do the same thing with three.
Go to three interface controller, command V to paste, change one to three. Now let's go back to interface controller again. And now I'm going to do it on the second method here will activate and just after super will activate, I want to command V again and I'm going to change one to will activate, copy that, and add it to two and three just like I did the first one.
This becomes three. Go back to interface controller and do the same thing for deactivate. Copy that. Paste it. Two.
And there's three. Go ahead and command build for this and then run. You'll see we got some things going on down here in the console so I'm going to click on here and drag this up a little bit so we can see what's going on and put the simulator back into place. And you can see what's going on. We've got one will activate, three will activate, and then we have two will activate and two will deactivate.
So it checked two but if I go over here to two again, you'll see two activates, three did deactivate because three is now hidden and two is shown. If I go back to three, three activates 'cause that's the one that's now going to show and two deactivated so that's all good and fine. Now let's go look at why that error message discourages programming pages. Click back into the code. Go to interface controller dot swift and let's put one into the array like that.
Stop the simulator, remember all your quotes. That's better, and run. I'm going to move the watch over a little bit here and I'm going to head over to the memory here. Watch this number here. It's increasing rather rapidly. You see over here in the console, the only thing you see is one awake and a lot of them.
And this should blow out any second now with a critical memory error. So why is it doing that? Let's go back to that code again. Awake initializes things. I let names here equal one, three, and two. So I present the one controller. That awakes one controller which presents the one controller and that awakes one controller which presents the one controller, each time taking more and more memory as we're doing this and you get into a very vicious, essentially recursive or memory leak situation where you never get anywhere so it just keeps waking those sets of controllers over and over again, trashing our memory.
The point here is simple. Never present the page that you're on and that's best prevented by sticking to segways. If you need all three pages displayed programmatically you make a fourth launching controller for the three pages. You're best off by using segways for pages. Comment out those two lines of code that we made then go back to the storyboard.
Drop the console out and stick your segways back in with control drags. Stop the simulator and run it again. Now in the console, we have zero errors and you can see how this all works. It goes from one awake, two awake, three awake. So it awakes all three controllers. It then activates one, activates two, shuts down two, activates three, deactivates three, and then when we go ahead and drag, it'll activate and deactivate according to the page we're on.
To summarize the life cycle for initialization, use awake. If you want to display before the view appears, use will activate. If you want to clean up when the controller leaves the view, use did deactivate.
- Principles of watchOS development
- Adding buttons and labels to your app
- Connecting objects to code
- Testing a watchOS app
- Laying out WatchKit UI objects
- Adding navigation
- Using Interface Library elements such as inputs, pickers, and media players
- Creating tables
- Working with table data