自定义UIView和becomeFirstResponder

自定义UIView和becomeFirstResponder

本文介绍了自定义UIView和becomeFirstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现 UIKeyInput 协议的自定义UIView,并且

I have a custom UIView that implements the UIKeyInput protocol and has

- (BOOL) canBecomeFirstResponder{
    return YES;
}

在子类中定义。致电时:

defined in the subclass. When calling:

[customView becomeFirstResponder];
NSLog(@"is first? %i",[customView isFirstResponder]);

返回false,即使 canBecomeFirstResponder 已正确设置,并且实现了所有 UIKeyInput 协议功能。还有什么东西阻止这种观点成为第一个响应者?如果有帮助,它会存在于scrollView和另一个自定义视图中。

during a button click, it returns false, even though canBecomeFirstResponder is properly set and all of the UIKeyInput protocol functions are implemented. What other things could be blocking this view from becoming the first responder? It lives inside of a scrollView and another custom view if that helps.

更新:

我检查了当前的第一响应者是什么:

I checked to see what the current first responder was with:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

令人惊讶 firstResponder 。所以似乎没有什么可以阻止事件。

and surprisingly firstResponder was nil. So nothing seems to be hogging the events.

推荐答案

你是否覆盖 becomeFirstResponder

跟进:

如果当前$ b,响应者对象只会成为第一个响应者$ b响应者可以辞退第一响应者状态(canResignFirstResponder)
并且新响应者可以成为第一响应者。

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.

您可以调用此方法来创建响应者对象比如查看
第一响应者。但是,如果
是视图层次结构的一部分,则只应在该视图上调用它。如果视图的window属性包含
UIWindow对象,则它已安装在视图层次结构中;如果
返回nil,则视图将从任何层次结构中分离。

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

您确认是否符合上述所有条件?

Did you verify you meet all of the above conditions?

这篇关于自定义UIView和becomeFirstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:11