开发者

how to return a value to the controller from a re-usable view

Specific question based on my implementation is:

Question:

  • have a UIViewController which has a some views within it (configured in an XIB file)
  • when something occurs in one of the controls on the screen here (i.e. in one of the views), how can I update a UIViewController parameter at the same time?

Background:

  • In my case I have a colorPickerController, and within this there are some subview which the user interacts with the select the color
  • when the user is dragging the mouse over one of the subviews, one of the other others is updated with the color
  • what I'm not sure of is how to also keep the main colorPickerController parameter updated with the latest color that is being selected
  • hence the question how to up开发者_如何学编程date/access a controller's parameter from within one of the views within a controller


The subview the user drags mouse (or finger?) over can be considered as some kind of a control, so you would just use target/action in that case. or may be a delegation. that way you would keep this subview and viewcontroller loosely coupled, and that would also allow you to reuse that same class (which the subview is instance of) in other places. so, in case of delegation:

  1. define a protocol:

    @protocol ColorPickerViewDelegate

    • (void)colorPickerView:(ColorPickerView *)cpView didUpdateWithColor:(UIColor *)newColor; @end
  2. add property @property(nonatomic, assign) id<ColorPickerViewDelegate> didUpdateDelegate; to color picking UIView subclass.

  3. Make your viewcontroller adopt the above protocol and define the method from that protocol in viewcontroller's implementation section.

  4. When creating a color picking subview, set your viewcontroller as that subview's didUpdateDelegate.

  5. make calls to didUpdateDelegate at appropriate times in your color picking view touch-handling logic: [didUpdateDelegate colorPickerView:self didUpdateWithColor:someNewColor];

hope that's what you want


I guess there is no any ColorPickerController in iOS Control Library ? Is it custom controller ? Please specify so can answer accordingly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜