Raunak
Raunak

Reputation: 3414

Is it bad design to set self.delegate = self

I have a UIViewController subclass (say MyViewController).

MyViewController.h

@protocol TargetChangedDelegate
    -(void) targetChanged; 
@end

@interface MyViewController

@property (weak) id<TargetChangedDelegate> targetChangedDelegate;

-(void) doSomethingOnYourOwn;

@end

MyViewController.m

@implementation MyViewController <TargetChangedDelegate>

-(void) doSomethingOnYourOwn
{
  // DO some stuff here

  // IS THIS BAD ??
  self.targetChangedDelegate = self;
}

-(IBAction) targetSelectionChanged 
{
  [self.targetChangedDelegate targetChanged];
}

-(void) targetChanged
{
   // Do some stuff here
}

@end

Based on certain conditions a class that instantiates an instance of MyViewController may decide to set itself as the delegate or not.

Foo.m

@property(strong) MyViewController *myVC;

-(void) configureViews
{
  self.myVC = [[MyViewController alloc] init];
  [self.view addSubview:self.myVC];

  if (someCondition) 
  {
    self.myVC.targetChangedDelegate = self;
  }
  else
  {
    [self.myVC doSomethingOnYourOwn]
    //MyViewController sets itself as the targetChangedDelegate
  } 

}

With reference to the code snippet above, I have the following question: Is it a violation of MVC/delegation design pattern (or just a bad design) to say:

self.delegate = self;

Upvotes: 3

Views: 2341

Answers (3)

malhal
malhal

Reputation: 30582

It is bad design to set self.delegate = self; it should be another object. Delegation via protocols are an alternative design to subclassing and you can read more about delegation here:

https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

And here is more on protocols: https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html

Upvotes: 0

JeremyP
JeremyP

Reputation: 86651

There's absolutely no problem with setting the delegate to self. In fact it is a good way to provide default delegate functionality if a delegate is not set by somebody else.

Obviously, the delegate property has to be declared weak otherwise you get a reference cycle.

To expand a bit, having read the wrong answer and wrong comments above, if you allow an object to be its own delegate, your code is cleaner because you do not have to surround absolutely every single delegate call with

if ([self delegate] != nil)
{
    [[self delegate] someMethod];
}
else
{
    [self someMethod];
}

Upvotes: 8

PlusInfosys
PlusInfosys

Reputation: 3446

Its not proper way to assign self.delegate = self. for your functionality, you can do this:

-(void) doSomethingOnYourOwn
{
  // DO some stuff here


  self.targetChangedDelegate = nil;
}

and when using delegate:

if(self.targetChangedDelegate != nil && [self.targetChangedDelegate respondsToSelector:@selector(targetChanged)]
{
   [self.targetChangedDelegate targetChanged];
}
else
{
     [self targetChanged];
}

Upvotes: 0

Related Questions