简体   繁体   English

设置self.delegate = self是不好的设计

[英]Is it bad design to set self.delegate = self

I have a UIViewController subclass (say MyViewController). 我有一个UIViewController子类(例如MyViewController)。

MyViewController.h MyViewController.h

@protocol TargetChangedDelegate
    -(void) targetChanged; 
@end

@interface MyViewController

@property (weak) id<TargetChangedDelegate> targetChangedDelegate;

-(void) doSomethingOnYourOwn;

@end

MyViewController.m 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. 基于某些条件,实例化MyViewController实例的类可以决定是否将其自身设置为委托。

Foo.m 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: 参考上面的代码片段,我有以下问题:是说违反了MVC /委托设计模式(或只是一种不良设计):

self.delegate = self;

There's absolutely no problem with setting the delegate to self . 将委托设置为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. 显然,必须将delegate属性声明为weak否则您将获得引用循环。

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];
}

Its not proper way to assign self.delegate = self. 分配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];
}

It is bad design to set self.delegate = self; 设置self.delegate = self;是不好的设计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 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 以下是有关协议的更多信息: https : //developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM