objective-cprotocolsuikeyinput

Why does a certain Protocol work without a class that implements it?


I have worked quite a lot with protocols like delegates and data sources and I recently worked on something using the UIKeyInput protocol. I am used to things like self.tableView.delegate = self but with UIKeyInput I didn't need to that. But the protocol methods I implemented (e.g. insertText: etc.) got called nevertheless.

Can someone explain to me how this works?

Thanks very much for your help!


Solution

  • The Cocoa Touch framework has a concept of the "responder chain", which is how it figures out where to deliver UI input events. The "first responder" is at the head of the responder chain. When iOS has a keyboard input event, it checks if the current first responder supports the UIKeyInput protocol and calls those methods if it does.

    You don't need to register as the UIKeyInputDelegate for another object, because the framework defines the recipient for those messages as the first responder.