Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to use <code>delegation</code> - like when you supply the data for a <code>UITableView</code> it doesn't know exactly what your class is, just that it implements a specified <code>@protocol</code>.</p> <p>Define your own protocol which describes the callback offered (this goes in ColorClass.h):</p> <pre><code>@protocol ColorClassDelegate &lt; NSObject &gt; - (void)colorPicker:(ColorClass *)colorPicker didPickColor:(UIColor *)color; @end </code></pre> <p>Then in your <code>ColorClass</code> (again, the .h file) you have a property for the delegate:</p> <pre><code>@property (weak, nonatomic) id &lt; ColorClassDelegate &gt; delegate; </code></pre> <p>When the colour has been chosen (button pressed):</p> <pre><code>- (void) buttonPressed : (id) sender { // delegate [self.delegate colorPicker:self didPickColor: ## get the colour here ## ]; // persistent store [myStorage setButtonColor:[sender tag]]; } </code></pre> <p>And any class that uses the colour picker implements the <code>ColorClassDelegate</code> protocol</p> <pre><code>#import "ColorClass.h" @interface ClassA &lt; ColorClassDelegate &gt; </code></pre> <p>And sets itself as the colour pickers delegate. Then it implements:</p> <pre><code>- (void)colorPicker:(ColorClass *)colorPicker didPickColor:(UIColor *)color { // do something with the colour } </code></pre> <p>Your original code was passing the button tag to represent the colour. You could do that instead of passing the colour in the delegate method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload