如何从可重用的视图向控制器返回值

时间:2011-06-24 11:11:34

标签: iphone ios uiview uiviewcontroller

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

问题:

  • 有一个UIViewController,其中有一些视图(在XIB文件中配置)
  • 当屏幕上的某个控件出现问题时(即在其中一个视图中),如何同时更新UIViewController参数?

背景:

  • 在我的情况下,我有一个colorPickerController,在其中有一些用户与选择颜色交互的子视图
  • 当用户将鼠标拖过其中一个子视图时,其他一个用其他颜色更新
  • 我不确定如何使用正在选择的最新颜色更新主colorPickerController参数
  • 因此问题如何从控制器内的一个视图中更新/访问控制器的参数

2 个答案:

答案 0 :(得分:1)

用户拖动鼠标(或手指?)的子视图可被视为某种控件,因此在这种情况下您只需使用目标/操作。或者可能是一个代表团。这样你就可以保持这个子视图和viewcontroller松散耦合,这也可以让你在其他地方重用同一个类(子视图的实例)。所以,如果是授权:

  1. 定义协议:

    @protocol ColorPickerViewDelegate

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

  3. 让你的viewcontroller采用上述协议,并在viewcontroller的实现部分中定义该协议的方法。

  4. 创建颜色选择子视图时,将viewcontroller设置为该子视图的didUpdateDelegate。

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

  6. 希望这就是你想要的

答案 1 :(得分:0)

我猜iOS控制库中没有任何ColorPickerController?是自定义控制器吗?请注明,以便相应地回答。