Imagine writing an app holding recipes, sponsored by the National Board for Healthy Eating.
Your sponsors have provided you with 10 recipes of steamed vegetables and one explaining how to make a delicious dessert of cream, marzipan and jam.
The sponsor's requirements are:
- Recipes must be shown with metric/imperial units depending on the user's preferences.
- Rumours have it that some people are not naturally excited by steamed vegetable recipes, so every 3 page views of a recipe, an animation of a cute rabbit eating a carrot must be shown at the bottom of the screen to make the app more popular.
- When the user has viewed 5 recipes of steamed vegetables, they are entitled to see the dessert recipe. Once.
To meet these requirements, you can use the methods/notifications in this way:
On viewWillAppear
, calculate the weights and volumes in either metric or imperial units and put the correct text in the various labels and text fields on the recipe page. This guarantees that the texts already are updated when shown to the user. If you do it in viewDidAppear
, there will be a brief visible flash when the text is updated.
On viewDidAppear
, you know that the recipe is correctly set up and visible to the user on the screen. This means that you can start the animation of the rabbit if the conditions are met. If you started the animation in viewWillAppear
, it will start too early.
Lastly, if the sneaky user tries to click on the dessert recipe, you will receive the tableView:shouldSelectRow:
message. Check if the conditions are met (if they have already viewed 5 steamed vegetables recipes) and if they are, return YES. Otherwise return NO, and the row selection will be cancelled by the system (and the dessert recipe not instantiated and shown).