views:

116

answers:

2
- (id)initWithDelegate:(id)delegate
{
    self = [super init];
    if (self)
    {
        _delegate = delegate;

    }
    return self;
}

Is it generally recommended to pass in the delegate in the init method? what about in another method that will be called first?

Also do I need to set _delegate to nil or release in the dealloc or viewDidUnload method? and what about the dealloc or viewDidUnload method of the controller implementing this delegate?

+1  A: 

Is it generally recommended to pass in the delegate in the init method?

That depends on what you want to do. If you will require a delegate every time your object is instantiated, it would be convenient to just pass it in when you init your object. It's all personal preference, though.

In the code here you are just assigning _delegate, so there is no need for you to do anything with that particular ivar in your dealloc or viewDidUnload methods.

kubi
A: 

Is it generally recommended to pass in the delegate in the init method? what about in another method that will be called first?

You should only set the delegate at init when the object might need to communicate with the delegate during initialization, or if the delegate should only be set once during the life of the object. Otherwise it is recommended to set the delegate though a property.

Also do I need to set _delegate to nil or release in the dealloc method? and what about viewDidUnload?

No, _delegate does not need to be set to nil and it only needs to be released if it has been retained.

jojaba
Setting the delegate in the init method can also be convenient. If you always follow up an init call by setting the property, you might as well merge then into one method call.
kubi