Note that there are some explanatory texts on larger screens.

plurals
  1. POobjective-c delegates and events design (I don't get it)
    text
    copied!<p>I'm pretty new to the objective-c language (less than three months) but it is something that i really need to understand.</p> <p>Suppose there is a controller (in a iOS environment) that manages a table view for input data from the user. The table must have editable cells and some features to make the value selection easier, for example a button that shows a popover with the possible values for a field.</p> <p>Suppose there is a field to store country names. The popover first shows a list of continents; when the user selects a continent, the controller of the popover must show the countries of the previews selected continent.</p> <p>Now, this popover appears in many places in the app so it will be nice if I can encapsulate it for later use. What i will expect for this popover is something like this:</p> <pre><code>... @protocol MyPopoverDelegate&lt;NSObject&gt; { -(void)didSelectCountry:(NSString *)countryName; { ... MyPopoverController *dataSelector = [[MyPopoverController] alloc] init]; dataSelector.dataType = CountryDataType; dataSelector.delegate = self; [dataSelector show]; [dataSelector release]; ... </code></pre> <p>The problem here is the line <code>[dataSelector release]</code> because the code for managing the popover must stay alive until the country is selected. That's means the <code>dataSelector</code> variable must be a property of the caller class and that sucks.</p> <p>The question then is: How can i organize situations like this to have a reusable controller?</p> <p>Thanks</p> <p><em>Edited after vodkhang answer:</em></p> <p>Ok, that's a good one, but <code>dataSelector</code> still is a property. What if i do:</p> <pre><code>@implementation MyPopoverController - (id)init { ... [self retain]; ... } - (void)popoverControllerDidDismissPopover: (UIPopoverController *)popoverController { ... [delegate didFinishSelectingCountry:countryName]; [self release]; } @end </code></pre> <p>I never see this behavior in objective-c, i feel that this is not the idea. Why is it wrong?.</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