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:
UIView
in a storyboard.Created a class that extends UIView
for this view, and override the methods canBecomeFirstResponder:
and becomeFirstResponder
to return YES
.
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.
答案 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?