簡體   English   中英

如何正確地將UITextField設置為第一響應者? (嘗試了3種方法)

[英]How can I make a UITextField the first responder, correctly? (Tried 3 methods)

設置如下:

SNController是一個NSObject ,它具有以下內容:

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

該視圖與ViewController中的視圖相同。

當時可以看到一個。 UITextField可見時(alpha = 1.0),所有其他UIUI都不可見(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];

它只是不會放棄,無論如何都不會成為第一響應者。

有任何想法嗎?

我意識到有一個錯誤。 SNController內部沒有UITextField,而是一個名為SNTextField的UIView子類。

在初始化UITextField的地方實現了此方法:

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

而且有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM