Appearing and Disappearing Views

UIViewController has several methods that get called at certain times:

viewWillAppear:

when its view is about to be added to the window

viewDidAppear:

when its view has been added to the window

viewWillDisappear:

when its view is about to be dismissed, covered, or otherwise hidden from view

viewDidDisappear:

when its view has been dismissed, covered, or otherwise hidden from view

These methods are useful because a view controller is only created once, but its view usually gets displayed (and dismissed or hidden) several times. You often need a way to override the default behavior at these times in the life of view controller. For example, you may want to do some sort of initialization each time the view is moved on screen. Here you would use viewWillAppear: or viewDidAppear:. Similarly, if you had a large data structure that you only need while the view controller is being displayed, you might want to do some clean-up each time the view controller is moved off screen. Then you would use viewWillDisappear: or viewDidDisappear:.

Note that these methods, as defined in UIViewController, do nothing. They are there so that your subclasses can override them.

 ​ ​ ​ ​ ​ ​ ​-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​;​
 ​ ​ ​ ​ ​ ​ ​-​ ​(​v​o​i​d​)​v​i​e​w​D​i​d​A​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​;​
 ​ ​ ​ ​ ​ ​ ​-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​D​i​s​a​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​;​
 ​ ​ ​ ​ ​ ​ ​-​ ​(​v​o​i​d​)​v​i​e​w​D​i​d​D​i​s​a​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​;​

Now let’s override viewWillAppear: to initialize the time label of the CurrentTimeViewController to the current time each time it is displayed and viewWillDisappear: to log to the console. In CurrentTimeViewController.m, make the following changes:

-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​C​u​r​r​e​n​t​T​i​m​e​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​w​i​l​l​ ​a​p​p​e​a​r​"​)​;​
 ​ ​ ​ ​[​s​u​p​e​r​ ​v​i​e​w​W​i​l​l​A​p​p​e​a​r​:​a​n​i​m​a​t​e​d​]​;​
 ​ ​ ​ ​[​s​e​l​f​ ​s​h​o​w​C​u​r​r​e​n​t​T​i​m​e​:​n​i​l​]​;​
}​
-​ ​(​v​o​i​d​)​v​i​e​w​W​i​l​l​D​i​s​a​p​p​e​a​r​:​(​B​O​O​L​)​a​n​i​m​a​t​e​d​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​C​u​r​r​e​n​t​T​i​m​e​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​w​i​l​l​ ​D​I​S​a​p​p​e​a​r​"​)​;​
 ​ ​ ​ ​[​s​u​p​e​r​ ​v​i​e​w​W​i​l​l​D​i​s​a​p​p​e​a​r​:​a​n​i​m​a​t​e​d​]​;​
}​

Build and run the application. Note that each time you return to the Time screen, the time label is updated. And each time you leave that screen, you will see the log statement in the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20