https://developer.apple.com/documentation/uikit/uiresponder

Responder objects—that is, instances of UIResponder —constitute the event-handling backbone of a UIKit app. Many key objects are also responders, including the UIApplication object, UIViewController objects, and all UIView objects (which includes UIWindow). As events occur, UIKit dispatches them to your app's responder objects for handling.

Responder 객체는 UIResponder의 인스턴스 입니다. 이 인스턴스가 UIKit에서 이벤트 처리의 중추역할을 합니다. UIKit의 많은 객체들, UIApplication, UIViewController, UIView 같은 객체들이 Responder이기도 합니다. 이벤트가 발생하면 UIKit는 이벤트를 Responder 객체에게 전달을 합니다.

There are several kinds of events, including touch events, motion events, remote-control events, and press events. To handle a specific type of event, a responder must override the corresponding methods. For example, to handle touch events, a responder implements the touchesBegan(:with:), touchesMoved(:with:), touchesEnded(:with:), and touchesCancelled(:with:) methods. In the case of touches, the responder uses the event information provided by UIKit to track changes to those touches and to update the app's interface appropriately.

화면을 터치하거나 제스쳐를 취하는 등 여러 이벤트가 있습니다. 어떤 특정한 이벤트를 처리하려면 해당이벤트에 대한 메서드를 Responder 객체가 override 해서 내용을 구현해야 합니다. 예를 들어 터치이벤트가 발생하면 responder 객체는 UIKit에서 제공하는 정보를 기반으로 앱의 인터페이스를 업데이트 하게 됩니다.

In addition to handling events, UIKit responders also manage the forwarding of unhandled events to other parts of your app. If a given responder doesn’t handle an event, it forwards that event to the next event in the responder chain. UIKit manages the responder chain dynamically, using predefined rules to determine which object should be next to receive an event. For example, a view forwards events to its superview, and the root view of a hierarchy forwards events to its view controller.

이벤트 처리 뿐만 아니라, UIKit의 responder들은 자신이 처리하지 않는 이벤트를 다른 요소들에게 전달하는 역할도 수행합니다. 만약에 어떤 responder객체가 이벤트를 처리하지 않으면, 이 객체가 Responder Chain의 다음 responder 객체에 이벤트를 전달합니다. Responder Chain은 UIKit에 의해 미리 정해진 규칙에 따라 동적으로 관리됩니다. 예를 들어 View는 자신의 Super View에게 이벤트를 전달하고 최상위에 있는 View는 ViewController 에게 이벤트를 전달합니다.

Responders process UIEvent objects but can also accept custom input through an input view. The system's keyboard is the most obvious example of an input view. When the user taps a UITextField and UITextView object onscreen, the view becomes the first responder and displays its input view, which is the system keyboard. Similarly, you can create custom input views and display them when other responders become active. To associate a custom input view with a responder, assign that view to the inputView property of the responder.

Responder 객체들은 UIEvent 객체들을 처리하지만 input View 를 통해서 커스텀 입력을 받을 수도 있씁니다. 가장 명확한 예는 키보드인데, 사용자가 화면에 있는 UITextField와 UITextView를 터치시 해당 뷰는 First Responder가 되어 입력 뷰, 즉 키보드를 표시합니다. 이처럼 개발자는 커스텀 입력뷰를 만들어서 어떤 responder 객체가 활성화 되면 화면에 표시될 수 있도록 할 수 있습니다. 커스텀 입력뷰를 responder 객체의 InputView property 에 넣어주면 됩니다.

<aside> 💡

  1. UIView, ViewController 등 많은 UIkit의 객체들은 UIResponder 인스턴스이다. 👉 Responder
  2. UIKit은 UIResponder 객체들을 연결해서 관리한다. 👉 Responder Chain
  3. UIkit은 앱에서 화면 터치와 같은 이벤트가 발생하면 Responder에게 이벤트를 보낸다.
  4. 이벤트를 받은 Responder들은 자신이 처리하거나 Responder Chain에 있는 자신의 다음 Responder에게 이벤트를 전달한다
  5. First Responder로 지정된 Responder는 이벤트를 가장 먼저 전달받는 Responder이다. 👉 First Responder </aside>

becomeFirstResponder()를 사용하는 이유!

해당 윈도우에서 이 객체를 first responder로 만들 것을 요청하는 함수 but! 호출한다고 해서 무조건 first responder가 된다는 것은.. 아닙니다. UIKit에게 현재 first reponder에게 resign을 요청하지만 원하는대로 되지 않을 수 있기 때문이라네요..

엔터를 눌렀을때 포커스 되어있는 텍스트 필드를 변경하는것이 아니라, 원하는 텍스트필드를 키보드에서 발생하는 이벤트에 대한 First Responder로 텍스트를 지정해주는 것 화면이 탭 되고 이벤트가 발생하면 이 이벤트가 first responder로 지정된 UITextView에 전달되면서 응답시 first responder 로 지정되어 필드에 글자가 채워지게 되는것이다.

이벤트 처리과정