[英]How can I make a UITextField the first responder, correctly? (Tried 3 methods)
設置如下:
SNController是一個NSObject ,它具有以下內容:
該視圖與ViewController中的視圖相同。
當時可以看到一個。 當UITextField可見時(alpha = 1.0),所有其他UIUI都不可見(alpha = 0.0)。
(1)我執行動畫,並且想要文本字段:
UITextField *textField;
在動畫發生時成為第一響應者:
[textField becomeFirstResponder];
[UIView animateWithDuration: ...];
不幸的是,它不起作用。
我已經記錄了文本字段,它表明它不能成為第一響應者,我也不知道為什么...
我嘗試了另外兩種方法,但到目前為止它們都沒有起作用:
(2)一種從SNController與ViewController通信的復雜方法:
在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];
它只是不會放棄,無論如何都不會成為第一響應者。
有任何想法嗎?
我意識到有一個錯誤。 SNController內部沒有UITextField,而是一個名為SNTextField的UIView子類。
我在初始化UITextField的地方實現了此方法:
- (void)activateKeyboard
{
[self.textField becomeFirstResponder];
}
而且有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.