gpt4 book ai didi

ios - 快速访问容器按钮

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:21 24 4
gpt4 key购买 nike

我对 Swift 比较陌生。这是我的问题:我有一个 UIViewController,里面有一个容器。容器有自己的 ContainerViewController。容器是应用程序的标题,其中有一个按钮。

我想根据按钮(容器内)是否被按下来设置 UIViewController 内的 Bool 值。

我不认为通过 segue 传递“mainVC”会成功:没有必要进行 segue,因为两个 View 显示在同一屏幕上。

我是否应该为 View 及其容器提供相同的 ViewControllerClass?

非常感谢您的宝贵时间

最佳答案

有不同的解决方案:

  1. ContainerViewControllerparentViewController(=UIViewController) 上调用方法
  2. 使用响应链:
    • UIViewController(子类)中实现canBecomeFirstResponder()返回YES
    • 在父 View Controller 中实现一个buttonPressed(sender:AnyObject)(或其他)方法
    • 从容器 View Controller 中调用 UIApplication.sharedApplication().sendAction("buttonPressed:", to: nil, from:self)

这将允许该方法“遍历”响应者链,直到它找到一个实现了 buttonPressed() 方法的对象。如果您想在其他 View Controller 中重用 header View ,此解决方案会很有用。

  1. 使用委托(delegate)模式。在容器 View Controller 中创建委托(delegate)属性 + 协议(protocol),并让父 vc 在 embed-segue 触发的 prepareForSegue() 方法中将自己设置为委托(delegate)。这也获得了可重复使用的标记。

  2. 如果您正在使用 Storyboard,那么您也可以使用 Unwind Segue

关于ios - 快速访问容器按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27951772/

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