iosuiresponder

How to set the UIView become first Responder?


I am learning UIResponder now. I wrote some test code. In my code I want to let the UIView become the first responder, but I failed. My test steps are:

  1. Create a UIView in a storyboard.
  2. Created a class that extends UIView for this view, and override the methods canBecomeFirstResponder: and becomeFirstResponder to return YES.

  3. In the method of the view controller's viewDidAppear: method, test it:

    - (void) viewDidAppear:(BOOL)animated {
        [self.viewD becomeFirstResponder];
        NSLog(@"%d ", [self.viewD isFirstResponder]);
    }
    

The result of isFirstResponder is always NO.

I have no idea why. Can anybody help me? Thanks.


Solution

  • becomeFirstResponder Notifies the receiver that it is about to become first responder in its window. YES if the receiver accepts first-responder status or NO if it refuses this status. The default implementation returns YES, accepting first responder status.

    A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder.

    In your case your view refuses the status that's why its NO every time.