How to set the UIView become firs tResponder?

时间:2016-02-03 03:14:27

标签: ios uiresponder

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.

2 个答案:

答案 0 :(得分:1)

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.

答案 1 :(得分:0)

so in your overrides make sure your calling super

also you can try to make sure that view is in front and visible you can try to end editing of the current parent view before calling becomeFirstResponder

[self.view bringSubviewToFront:self.D];
  [self.view endEditing:true];
  [self.D becomeFirstResponder];

also any reason your not calling super in your ViewDidAppear?