When Delegate Methods been called?

Hi, I’m at My Locations App in iOS Apprentice. And I just wonder when the delegate methods be called?

I know that when define a new method is func FUNCNAME() { } and call the method is just FUNCNAME(), but in the book.

I saw that after the delegate methods are been defined, they never been called, at least never been called by the FUNCNAME() way, I know they are not normal methods, but I just wonder when they get called?

And what if I write my own delegate and delegate methods, in Checklist app, when write my own delegate methods, I didn’t call them use the FUNCNAME() way either.

I am not sure what you mean here. I think you are trying to ask how/when the delegate methods are called?

If yes, then that depends on the the class using the delegate. For example, in Checklists, you have the ListDetailViewControllerDelegate which is adhered to by AllListsViewController. So you have all the methods in AllListsViewController. However, the methods are called from ListDetailViewController which has a delegate of type ListDetailViewControllerDelegate.

Check the code for ListDetailViewController to see how (and where) the methods are called.

That is the way iOS is structured: you define methods, then iOS calls them at the appropriate time. For instance, in the very first app–the BullsEye app–your code never calls the function that is hooked up to the Hit Me! button: nowhere in your code do you see showAlert() being called. All you see is the definition of showAlert(). But when the user clicks on the Hit Me! button, iOS knows to call showAlert().

In the Checklists app, nowhere in your code do you call the function tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath). Instead, you define the function, and when the user selects a row then iOS calls the tableView function you defined in your code.

I just wonder when the delegate methods be called

The tableView method mentioned earlier is a delegate method. A delegate method cannot be some randomly named method–like the action method for a button. Instead, iOS calls a method with a specific name in response to some event, and if we want our app to do something in response to that event, then we have to define a method with that specific name. Note that iOS will call a delegate method whether or not our code defines the method. In other words, if we don’t define a delegate method using the override keyword, then iOS will call its own implementation of the delegate method which performs some default response to the event. The override keyword tells iOS not to execute the default implementation of the delegate method, rather execute our version of the method instead.

How do we know the name of the delegate method that iOS is going to call in response to an event? We have to read the docs–or read a book that explains which methods are called.

iOS employs what’s called “event driven programming”. iOS sits there waiting for events like button clicks to happen or tableViews needing to display rows, then iOS calls the methods that we define in our code. What we study in iOS Apprentice is which methods iOS calls in response to various events.

[In] iOS, apps are event-driven, which means that the objects listen for certain events to occur and then process them. As strange as it may sound, an app spends most of its time doing… absolutely nothing. It just sits there waiting for something to happen. When the user taps the screen, the app springs to action for a few milliseconds and then it goes back to sleep again until the next event arrives. Your part in this scheme is that you write the source code for the actions that will be performed when your objects receive the messages for such events.

p 50, iOS Apprentice 6.0

Now, for code organizational purposes, i.e. in order to make our code neater and easier to understand, we might not want to put 20-30 lines of code inside an action method or a delegate method. In that case, we can define our own “helper” method, then we can call our helper method from inside the action method or the delegate method. In that case, your action methods or your delegate methods will contains lines like myFunc(), where you do actually call a method in your code.

I think I understand now, thanks very much.

This topic was automatically closed after 166 days. New replies are no longer allowed.