iOS - UITextView 成为FirstResponder

发布于 2024-12-18 06:37:21 字数 171 浏览 2 评论 0原文

这就是问题:当 MessageComposeViewController 被解雇时,我的 textView 不会成为第一响应者,键盘也不会出现。为什么?我将 [textViewBecomeFirstResponder] 代码放入 viewWillAppear 中。我该怎么办?

This is the problem : when MessageComposeViewController has been dismissed, my textView doesn't become the first responder and the keyboard doesn't appear. Why? I put the [textView becomeFirstResponder] code in viewWillAppear. How can i do??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如果没有你 2024-12-25 06:37:21

我后来用这句话调用了“becomeFirstResponder”:

[self.searchBar performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1f];

它对我有用。希望这有帮助。

估计是动画的原因吧

I called becomeFirstResponder later by this line:

[self.searchBar performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1f];

It worked for me. Hope this help.

I guess it's the animation's reason.

水染的天色ゝ 2024-12-25 06:37:21

根据 苹果官方文档

在将对象分配为第一响应者之前,请确保您的应用已建立其对象图。例如,您通常在 viewDidAppear: 方法的重写中调用BecomeFirstResponder 方法。如果您尝试在 viewWillAppear: 中分配第一响应者,则您的对象图尚未建立,因此变为FirstResponder方法返回NO。

您应该在 viewDidAppear: 中调用 [textViewBecomeFirstResponder],而不是 viewWillAppear:

似乎您在 [selfmissModalViewControllerAnimated:NO] 中找到的解决方案正在利用注释中的相同规则。

According to the Note from the official Apple documentation :

Make sure that your app has established its object graph before assigning an object to be the first responder. For example, you typically call the becomeFirstResponder method in an override of the viewDidAppear: method. If you try to assign the first responder in viewWillAppear:, your object graph is not yet established, so the becomeFirstResponder method returns NO.

you should call [textView becomeFirstResponder] in the viewDidAppear: instead of viewWillAppear:.

Seems the solution you have found with [self dismissModalViewControllerAnimated:NO] is utilizing the same rule from the note.

下壹個目標 2024-12-25 06:37:21

您的问题不是特别清楚,但您假设当模态视图控制器(我假设您称之为 MessageComposeViewController )被关闭时,会调用 viewWillAppear 。并非所有情况下都会出现这种情况(例如,如果您的视图位于 UINavigationController 内),

您可以通过多种方法来处理此问题,但也许最简单的方法是传递您的 MessageComposeViewController 对原始视图控制器的引用并调用一个方法以使您的 UITextField 成为第一响应者。

Your question isn't particularly clear, but you have assumed that viewWillAppear is called when a modal view controller (what I assume you have called MessageComposeViewController) is dismissed. This may not be the case under all cirumstances (e.g. if your view is inside a UINavigationController)

There are a number of ways you could handle this, but perhaps the simplest would be to pass your MessageComposeViewController a reference to your original view controller and call a method to make your UITextField the first responder.

吃颗糖壮壮胆 2024-12-25 06:37:21

我或多或少有相同的问题,它与 segue 处的动画有关,看起来像在不使用 resignFirstResponder 的情况下执行 segue o 关闭视图,破坏与视图的关系,我只是修改 segue 以像这样以编程方式执行此操作:

  - (IBAction)back:(id)sender {

     [textView resignFirstResponder];
     [self performSegueWithIdentifier:@"returnScreen" sender:self];
  }

然后在 viewWillAppear 中我做了下一个:

 -(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
     [textView becomeFirstResponder];

  ....

  }

I have more or less the same problem, it was related with animation at the segue, it looks like doing a segue o dismiss a view without using the resignFirstResponder break the relation with the view, I just modify the segue to doing it programatically like this:

  - (IBAction)back:(id)sender {

     [textView resignFirstResponder];
     [self performSegueWithIdentifier:@"returnScreen" sender:self];
  }

An then in the viewWillAppear I did the next:

 -(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
     [textView becomeFirstResponder];

  ....

  }
面犯桃花 2024-12-25 06:37:21

迅速

let when = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
    dispatch_after(when, dispatch_get_main_queue()) {
        self.searchBar.becomeFirstResponder()
    }

In swift

let when = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
    dispatch_after(when, dispatch_get_main_queue()) {
        self.searchBar.becomeFirstResponder()
    }
残龙傲雪 2024-12-25 06:37:21

我找到解决办法了!!!!!!!
因此,在 messageComposeViewController:didFinishWithResult 委托方法中,我们调用 [selfmissModalViewControllerAnimated:YES],但这是错误的!此方法应该收到 NO,而不是 YES!然后回忆一下[textViewBecomeFirstResponder]方法!我不知道为什么,但这样该应用程序就可以完美运行!
不过还是谢谢大家了! :)

I found the solution!!!!!!!!
So, in the messageComposeViewController:didFinishWithResult delegate method we call [self dismissModalViewControllerAnimated:YES], but it's wrong! This method should receive NO, not YES! And then recall the [textView becomeFirstResponder] method! I don't know why, but in this way the app works perfectly!
Thanks all however! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文