【问题标题】:How can I make a UITextField the first responder, correctly? (Tried 3 methods)如何正确地将 UITextField 设为第一响应者? (尝试了3种方法)
【发布时间】:2014-12-15 18:35:22
【问题描述】:

设置如下:

SNController 是一个 NSObject,它的内部有:

  1. UIView
  2. UITextField
  3. UISegmentedControl
  4. UILabel
  5. UIButton

视图与 ViewController 内的视图相同。

当时可以看到一个。 UITextField 可见时 (alpha = 1.0) 所有其他都是不可见的(alpha = 0.0)。

(1) 我执行动画并且我想要文本字段:

UITextField *textField;

在动画发生时成为第一响应者:

[textField becomeFirstResponder];

[UIView animateWithDuration: ...];

不幸的是,它不起作用。

我已经记录了文本字段,它显示它不能成为第一响应者,我不知道为什么......

我已经尝试了另外两种方法,但到目前为止都没有奏效:

(2) 从 SNControllerViewController 通信的复杂方式:

SNController.h/.m里面:

@property (nonatomic, copy) void(^eventCallBack)();

if (self.eventCallBack) {

    self.eventCallBack();
}

ViewController.m里面:

__weak ViewController *self_ = self;

self.restoration.controller.eventCallBack = ^(){

    [self_.restoration.controller.textField becomeFirstResponder];
};

(3) 我也从 SNController 内部尝试了这些方法:

[self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];

[self.textField performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil waitUntilDone:YES];

它不会放弃,无论如何它都不会成为第一响应者。

有什么想法吗?

【问题讨论】:

  • 显示您调用 becomeFirstResponder 的代码。
  • 什么意思? [textField becomeFirstResponder] 是代码...我还有另外 3 个 sn-ps。
  • 为了成为 firstResponder,文本字段必须位于窗口的视图层次结构中。
  • 您的代码 sn-ps 都没有提供任何类型的上下文。
  • 我必须提到尽可能多的东西才能自己找到答案。

标签: ios objective-c uitextfield uiresponder becomefirstresponder


【解决方案1】:

我意识到有一个错误。 SNController 内部没有 UITextField,而是一个名为 SNTextField 的 UIView 子类。

我在 UITextField 被初始化的地方实现了这个方法

- (void)activateKeyboard
{
    [self.textField becomeFirstResponder];
}

它成功了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多