Tutorial 2: Delegate Methods Question page 104

Hello,
in the delegate Methods in the ChecklistViewController.swift it is written:

func addItemViewControllerDidCancel(_ controller: AddItemViewController) {
    dismiss(animated: true, completion: nil)
}

I would have been tended to expect something like:

func addItemViewControllerDidCancel(_ controller: AddItemViewController) {
    controller.dismiss(animated: true, completion: nil)
}

How does swift know that its the delegate which should be dismissed and not
the ChecklistViewController in which dismiss is called.

Both are working, I just would like to know where the “magic” is coming from :blush:

Thanks for your help in advance
Sven

What is dismissed is the top-most view controller. You can tell that view controller to dismiss itself, but if you just call dismiss(...) instead of controller.dismiss(...) then UIKit will look up which is the top-most view controller and dismiss that. So it’s a little shorthand and the magic is provided by UIKit.

Either way of doing it is fine. :slight_smile:

Hi Matthijs,
thanks for your help.
If you know it, it makes sense :relaxed:

By the way I really like the way your tutorials are built.
There is no need to know every detail from the beginning.

At this point I was just puzzled. But that was just because of my limited knowledge of iOS.

Really looking forward to do the rest of the tutorials.
Thanks a lot.

Regards
Sven