title: Delegates in class methods
body: <p>Let's say I have a class utility method which initialises an object, assigns its delegate and terminates.
  Is there any way to keep that delegate around so that its not released causing a <code>message sent to deallocated instance</code> error?</p>
  
  <p>I understand that delegate objects are weakly referenced by their delegating object to avoid a retain cycle (because the delegate object normally already has a strong reference to the delegating object).</p>
  
  <p>In my case, my delegate object does not need to retain a reference to the delegating object after it is initialised (I think), so I avoid the the possibility of a retain cycle.</p>
  
  <p>So my question is, is there any way in ARC for me to set the delegate of an object (a UIAlertView, for example) and specify that <strong><em>it retains</em></strong> the delegate, and frees it whenever it is not used anymore (when control reaches the end of the UIAlertView delegate method, for example)?</p>
  
  <p>In case it helps, here's some example code:</p>
  
  <pre><code>+ (void)doSomethingWithCallback:(void(^)(void))callback
  {
      UIAlertViewDelegateWithBlock *delegate = [UIAlertViewDelegateWithBlock new];
      delegate.callback = callback;
      [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Hello World." delegate:delegate cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show];
  }
  </code></pre>
  
  <p><code>UIAlertViewDelegateWithBlock</code> is this:</p>
  
  <pre><code>@interface UIAlertViewDelegateWithBlock : NSObject &lt;UIAlertViewDelegate&gt;
  
  @property (strong, nonatomic)void(^callback)(void);
  
  @end
  
  @implementation UIAlertViewDelegateWithBlock
  
  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  {
      self.callback();
  }
  
  @end 
  </code></pre>
  
  <p>By the way, if the delegate object returns a strong reference to the caller, the delegate is retained and I get no errors. But having a variable used only to create a strong reference to the delegate is bad practice. </p>