iOS Swift:将焦点从一个文本字段移动到另一个文本字段

iOS Swift:将焦点从一个文本字段移动到另一个文本字段,ios,swift,textfield,becomefirstresponder,Ios,Swift,Textfield,Becomefirstresponder,我正在开发一个iOS应用程序,它使用PageViewController包装几个“聊天室”。我在每页中都有一个文本字段,当我滑动以移动到另一页时,我希望在不丢失键盘的情况下将焦点从当前页文本字段更改为新页文本字段 我尝试过不同的方法,但我无法做到这一点,每次我在新页面文本字段中调用becomeFirstResponder时,键盘会下降,然后立即上升,然后文本字段成为第一响应者 你能帮我吗 谢谢大家! 我找到了这个问题的解决方案: 在每个页面中使用一个变量 var firstResponderIs

我正在开发一个iOS应用程序,它使用PageViewController包装几个“聊天室”。我在每页中都有一个文本字段,当我滑动以移动到另一页时,我希望在不丢失键盘的情况下将焦点从当前页文本字段更改为新页文本字段

我尝试过不同的方法,但我无法做到这一点,每次我在新页面文本字段中调用becomeFirstResponder时,键盘会下降,然后立即上升,然后文本字段成为第一响应者

你能帮我吗


谢谢大家!

我找到了这个问题的解决方案:

在每个页面中使用一个变量

var firstResponderIsLocked: Bool = false
检查文本字段是否应结束编辑

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return !firstResponderIsLocked
}
当我在页面之间滑动时处理它:

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
    if let viewControllerToShow = pendingViewControllers[0] as? LiveMessagingPageContentViewController {
        pendingViewControllerIndex = viewControllerToShow.index
        lockFirstResponder(pendingViewControllerIndex!)
    }
}

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed && pendingViewControllerIndex != nil {
        takeFirstResponder(pendingViewControllerIndex!)
    }
    pendingViewControllerIndex = nil
}

我使我的应用程序能够根据需要工作。

我能够找到解决此问题的方法:

在每个页面中使用一个变量

var firstResponderIsLocked: Bool = false
检查文本字段是否应结束编辑

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return !firstResponderIsLocked
}
当我在页面之间滑动时处理它:

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
    if let viewControllerToShow = pendingViewControllers[0] as? LiveMessagingPageContentViewController {
        pendingViewControllerIndex = viewControllerToShow.index
        lockFirstResponder(pendingViewControllerIndex!)
    }
}

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed && pendingViewControllerIndex != nil {
        takeFirstResponder(pendingViewControllerIndex!)
    }
    pendingViewControllerIndex = nil
}

我使我的应用程序能够根据需要工作。

我能够找到解决此问题的方法:

在每个页面中使用一个变量

var firstResponderIsLocked: Bool = false
检查文本字段是否应结束编辑

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return !firstResponderIsLocked
}
当我在页面之间滑动时处理它:

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
    if let viewControllerToShow = pendingViewControllers[0] as? LiveMessagingPageContentViewController {
        pendingViewControllerIndex = viewControllerToShow.index
        lockFirstResponder(pendingViewControllerIndex!)
    }
}

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed && pendingViewControllerIndex != nil {
        takeFirstResponder(pendingViewControllerIndex!)
    }
    pendingViewControllerIndex = nil
}

我使我的应用程序能够根据需要工作。

我能够找到解决此问题的方法:

在每个页面中使用一个变量

var firstResponderIsLocked: Bool = false
检查文本字段是否应结束编辑

func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    return !firstResponderIsLocked
}
当我在页面之间滑动时处理它:

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
    if let viewControllerToShow = pendingViewControllers[0] as? LiveMessagingPageContentViewController {
        pendingViewControllerIndex = viewControllerToShow.index
        lockFirstResponder(pendingViewControllerIndex!)
    }
}

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed && pendingViewControllerIndex != nil {
        takeFirstResponder(pendingViewControllerIndex!)
    }
    pendingViewControllerIndex = nil
}

我让我的应用程序根据需要工作。

将文本字段放在页面视图控制器的视图上,而不是放在每个子页面上。从页面视图控制器中,调用
self.view.bringSubviewToFront(textField)
将其置于顶部


我刚刚在一个新项目(使用“基于页面的应用程序”模板)中对此进行了测试,效果很好。

将文本字段放在页面视图控制器的视图上,而不是每个子页面上。从页面视图控制器中,调用
self.view.bringSubviewToFront(textField)
将其置于顶部


我刚刚在一个新项目(使用“基于页面的应用程序”模板)中对此进行了测试,效果很好。

将文本字段放在页面视图控制器的视图上,而不是每个子页面上。从页面视图控制器中,调用
self.view.bringSubviewToFront(textField)
将其置于顶部


我刚刚在一个新项目(使用“基于页面的应用程序”模板)中对此进行了测试,效果很好。

将文本字段放在页面视图控制器的视图上,而不是每个子页面上。从页面视图控制器中,调用
self.view.bringSubviewToFront(textField)
将其置于顶部


我刚刚在一个新项目中测试了这个功能(使用“基于页面的应用程序”模板),效果很好。

您是否正在调用旧文本字段上的
resignFirstResponder
?如果你是,不要。我没有使用resignFirstResponder,你在旧文本字段上调用
resignFirstResponder
?如果你是,不要。我没有使用resignFirstResponder,你在旧文本字段上调用
resignFirstResponder
?如果你是,不要。我没有使用resignFirstResponder,你在旧文本字段上调用
resignFirstResponder
?如果你是,不要。我根本不使用resignFirstResponder谢谢你的awnser jrc。这是一个有效的解决方案,但我需要在每个子页面中都有一个textfield,因为我不想在切换到另一个页面时丢失在textfield中输入的文本(我可能也可以将文本保存在子页面变量中,但我发现我的当前解决方案没有问题),感谢您的awnser jrc。这是一个有效的解决方案,但我需要在每个子页面中都有一个textfield,因为我不想在切换到另一个页面时丢失在textfield中输入的文本(我可能也可以将文本保存在子页面变量中,但我发现我的当前解决方案没有问题),感谢您的awnser jrc。这是一个有效的解决方案,但我需要在每个子页面中都有一个textfield,因为我不想在切换到另一个页面时丢失在textfield中输入的文本(我可能也可以将文本保存在子页面变量中,但我发现我的当前解决方案没有问题),感谢您的awnser jrc。这是一个有效的解决方案,但我需要在每个子页面中都有一个textfield,因为我不想在切换到另一个页面时丢失在textfield中输入的文本(我可能也可以将文本保存在子页面变量中,但我在当前解决方案中发现ok)