在嵌套在 PageViewController 中的 viewController 上设置 becomeFirstResponder [英] Setting becomeFirstResponder on viewController nested in PageViewController

查看:28
本文介绍了在嵌套在 PageViewController 中的 viewController 上设置 becomeFirstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pageViewController,它有两个子视图控制器,您可以在它们之间滑动.其中一个有一个 textView,当您滚动到该页面时,我希望它成为第一响应者,然后在您滚动离开时失去焦点.现在我有这个:

I have a pageViewController that has two child view controllers that you can swipe between. One of them has a textView that I want to be the first responder when you scroll onto that page, then lose focus when you scroll away. Right now I have this:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    composeTextView.delegate = self
    composeTextView.becomeFirstResponder()
}

一旦您开始滚动到视图上,键盘就会出现.但是一旦滚动完成,键盘就会消失.

The keyboard shows up as soon as you start to scroll onto the view. But as soon as the scrolling finishes, the keyboard disappears.

任何想法为什么会发生这种情况?

Any ideas why this is happening?

推荐答案

它可以正常使用过渡样式 Page Curl 但不适用于 Scroll.

It works ok transition style Page Curl but not with Scroll.

我认为问题出在 viewDidAppear 之后,旧页面立即退出第一响应者并选择了一个新页面,因此您设置为 firstResponder 的任何内容都会丢失.这是由于旧视图和新视图在窗口中重复添加和删除而导致的,这会影响响应者链.我认为在旧视图滚出屏幕后,它会出于某种原因重新添加到窗口中,因为当您滚回它时,它已准备就绪.

I think the problem is immediately after viewDidAppear the old page resigns first responder and a new one is chosen thus whatever you set as firstResponder is lost. This is caused by the old and new view being repeatedly added and removed from the window which affects the responder chain. I think that after the old view scrolls off screen it is added back to the window for some reason because when you scroll back to it, it's ready to go.

正如您在此日志中看到的,在 viewDidAppear 之后,两个视图的窗口都有许多奇怪的变化.新视图是 0x7fd87fa24160 那么为什么旧视图 0x7fd87fa059a0 被删除并多次添加到窗口中?

As you can see in this log, after viewDidAppear there are many odd changes to both view's window. The new view is 0x7fd87fa24160 so why is the old view 0x7fd87fa059a0 being removed and added to the window multiple times?

2020-06-01 10:22:14.093911+0100 Paging2[45575:3540067] DetailViewController 0x7fd87fa15850 viewDidAppear: 0x7fd87fa24160
2020-06-01 10:22:14.094973+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x0
2020-06-01 10:22:14.095358+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x0
2020-06-01 10:22:14.096090+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x7fd87f80f3d0
2020-06-01 10:22:14.096511+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x7fd87f80f3d0
2020-06-01 10:22:14.099019+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x0
2020-06-01 10:22:14.099318+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x0
2020-06-01 10:22:14.099812+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x7fd87f80f3d0
2020-06-01 10:22:14.100306+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x7fd87f80f3d0

为了比较,这里是推送时的 UINavigationController.我在这个问题中了解到额外的移动到窗口用于过渡,打开慢速动画有帮助:

For comparison here is UINavigationController when pushing. I learned in this question that the extra move to window is for the transition, turning on slow animations helps:

2020-06-01 10:49:16.786872+0100 NavWindowTest[45792:3560379] View 0x7fbfba608090 didMoveToWindow 0x0 (current view removed from window)
2020-06-01 10:49:16.787170+0100 NavWindowTest[45792:3560379] View 0x7fbfba608090 didMoveToWindow 0x7fbfba50c150 (current view added to window for transition)
2020-06-01 10:49:16.787577+0100 NavWindowTest[45792:3560379] View 0x7fbfba5124d0 didMoveToWindow 0x7fbfba50c150 (new view added)
2020-06-01 10:49:21.801791+0100 NavWindowTest[45792:3560379] View 0x7fbfba608090 didMoveToWindow 0x0 (old view removed from transition)
2020-06-01 10:49:21.803690+0100 NavWindowTest[45792:3560379] ViewController 0x7fbfbd005dd0 viewDidAppear: 0x7fbfba5124d0

viewDidAppear 在导航时最后被调用但在分页时首先被调用的事实让我觉得有问题.

The fact that viewDidAppear is called last when navigating but first in paging makes me think there is a problem.

这篇关于在嵌套在 PageViewController 中的 viewController 上设置 becomeFirstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆