gpt4 book ai didi

ios - 暂停父 View Controller 的 UIGestureRecognizer

转载 作者:行者123 更新时间:2023-11-30 12:42:13 24 4
gpt4 key购买 nike

我在我的应用程序中添加了一个弹出窗口,它位于主父 View Controller 之上。但是当我在弹出窗口上滑动时,主视图 Controller 正在识别滑动。如何在弹出窗口打开时优先考虑 subview Controller 的操作或停止主视图 Controller 上的手势识别器?

这是我添加 subview 的代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedItem = self.menuItems[indexPath.row]
let ratingVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "rating") as! RatingView
ratingVC.mainView = self
ratingVC.foodName = selectedItem
self.addChildViewController(ratingVC)
ratingVC.view.frame = self.view.frame
self.view.addSubview(ratingVC.view)
ratingVC.didMove(toParentViewController: self)
}

最佳答案

感谢您添加代码!看起来我们应该改变 View Controller 的呈现方式。首先,我将解释此代码不起作用的简单原因:当此方法返回时,RatingVC 变量将被清除,并且由于它是对 View Controller 的唯一引用,因此该 View Controller 是从内存中清除。所以你的触摸不能发送给它:-)

有几种更好的方法来呈现 View Controller 。您的代码正在执行所谓的“ View Controller 包含”,除非您知道为什么要这样做,否则它可能有点矫枉过正。

您可以做的第一件事是将呈现的 View Controller 设置为此 TableView Controller 上的强属性,然后使用 presentViewController:animated:completion: 来呈现它。这很简单,Apple 的库可以将其 View 显示在屏幕上。

不过,现在推荐的方式是在点击 TableView 单元格后呈现 View Controller 的场景,如下所示:

  1. 使用 Storyboard 来设置表格 View (带有原型(prototype)单元格)和目标场景。
  2. 按住 Ctrl 键并从单元原型(prototype)拖动到目标场景。
  3. 在 TableView Controller 的代码中,使用 prepareForSegue:sender:为目标 View Controller 提供所需的状态。

顺便说一句,代码中的 ratingVC.mainView = self 可能暗示着另一种反模式。简单的建议是:确保 mainView 被标记为弱。更好的建议是使用松耦合设计模式,例如委托(delegate)、目标/操作或回调。

关于ios - 暂停父 View Controller 的 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42102043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com