gpt4 book ai didi

iphone - 如何从可重用 View 将值返回到 Controller

转载 作者:行者123 更新时间:2023-11-29 05:02:49 25 4
gpt4 key购买 nike

基于我的实现的具体问题是:

问题:

  • 有一个 UIViewController,其中包含一些 View (在 XIB 文件中配置)
  • 当屏幕上的某个控件(即其中一个 View )发生某些情况时,如何同时更新 UIViewController 参数?

背景:

  • 在我的例子中,我有一个 colorPickerController,其中有一些 subview ,用户可以与这些 subview 进行交互以选择颜色
  • 当用户将鼠标拖动到其中一个 subview 上时,其他 subview 之一会更新颜色
  • 我不确定如何使主要 colorPickerController 参数保持更新为所选的最新颜色
  • 因此问题是如何从 Controller 内的 View 之一更新/访问 Controller 的参数

最佳答案

用户拖动鼠标(或手指?)的 subview 可以被视为某种控件,因此在这种情况下您只需使用目标/操作。或者可以是一个代表团。这样您就可以保持 subview 和 View Controller 松散耦合,并且还允许您在其他地方重用相同的类( subview 是其实例)。因此,在委托(delegate)的情况下:

  1. 定义协议(protocol):

    @协议(protocol)ColorPickerViewDelegate

    • (void)colorPickerView:(ColorPickerView *)cpView didUpdateWithColor:(UIColor *)newColor;@结束
  2. 添加属性 @property(nonatomic, assign) id<ColorPickerViewDelegate> didUpdateDelegate;到 UIView 子类的颜色选取。

  3. 让您的 View Controller 采用上述协议(protocol),并在 View Controller 的实现部分定义该协议(protocol)的方法。

  4. 创建颜色选择 subview 时,将 View Controller 设置为该 subview 的 didUpdateDelegate。

  5. 在颜色选择 View 触摸处理逻辑中的适当时间调用 didUpdateDelegate:[didUpdateDelegate colorPickerView:self didUpdateWithColor:someNewColor];

希望这就是你想要的

关于iphone - 如何从可重用 View 将值返回到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6466920/

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