Chapter 22. Delegates are everywhere
This chapter covers
- Adding new LioNs
- Implementing the Delegate pattern
- Adding the like and dislike properties
You added a lot of functionality in the last chapter, but you still haven’t added the functionality you need to let a user create a new LioN. You’re going to do that in this chapter, but you’re also going to use delegates again. You learned about delegates in chapter 16, so refer back there if you need a refresher.
As you know, you’ve created the functionality that allows a user to tap the + button and open the AddEditViewController scene; and when the user taps Done or Cancel, they’re returned to the main screen. You want to add functionality that will allow you to save the text that the user has entered in the text fields and pass that back to create a new LioN object. If you think about it, though, in order to pass data between two views, you need to have references to those views. You could have your AddEditViewController set the LioN property in the MainViewController by doing something like this:
- Create a variable called mainVC that is of type MainViewController in your AddEditViewController.
- Add a function to create a new LioN (addLion) in the MainViewController.
- Call the mainVC.addLion function from AddEditViewController.