[英]Simple delegate method or property on destination view controller
This is a question regarding convention/best practice. 这是有关惯例/最佳实践的问题。 Say you have two view controllers
Foo
and Bar
. 假设您有两个视图控制器
Foo
和Bar
。 Foo
shows a list of things, and each thing has an image associated with it which Bar
is used to present. Foo
显示了一个事物列表,每个事物都有与之相关的图像, Bar
用来呈现该图像。 Eg Foo
might display a list of people names, and pressing on one of those names brings up a picture of that person using the Bar
controller. 例如,
Foo
可能会显示一个人的名字列表,然后按下其中一个名字,可以使用Bar
控制器显示该人的照片。 So Bar
requires a simple piece of information from Foo
- in this case, an image to display. 因此,
Bar
需要Foo
一条简单的信息-在这种情况下,需要显示一张图片。
There are two ways we could get the image to Bar
: 我们可以通过两种方法将图像发送到
Bar
:
i) set Foo
as Bar
s delegate, using some kind of BarDelegate
protocol we define, and have BarDelegate
have a mandatory method along the lines of - (UIImage *)imageToPresent;
i)使用我们定义的某种
BarDelegate
协议将Foo
设置为Bar
的委托人,并让BarDelegate
- (UIImage *)imageToPresent;
方式具有强制性方法- (UIImage *)imageToPresent;
ii) have a simple property on Bar
, eg @property (nonatomic, weak) UIImage *imageToPresent;
ii)在
Bar
上具有简单属性,例如@property (nonatomic, weak) UIImage *imageToPresent;
and have Foo
set this property before segueing to Bar
. 并让
Foo
设置此属性,然后再选择Bar
。
Is there a convention or preferred way out of these two? 这两者之间是否有约定或首选方式?
Best I can tell, the advantage of the delegate approach is that you can make explicit that there are some things that are required for Bar
to work. 据我所知,委托方法的优点是您可以明确指出
Bar
正常工作需要一些条件。 Sure, Foo
might forget to set itself as Bar
s delegate, but if Bar
had say 10 required properties, there's now still only one place to mess up (forgetting to set the delegate) rather than 10. The advantage of just using properties is that it makes the code more terse. 当然,
Foo
可能会忘记将自己设置为Bar
的委托,但是如果Bar
说了10个必需的属性,那么现在仍然只有一个地方搞砸了(忘记设置委托),而不是10个。使用属性的好处是它使代码更简洁。 I should note that I'm only interested in the case where the property in question is invariant across the destination view controller's lifecycle. 我应该注意,我只对目标属性在目标视图控制器的生命周期中不变的情况感兴趣。 I understand that delegation might have additional appeal if the property was variant across the lifecycle and the destination view controller needed to "query" the source view controller for it more than once.
我知道,如果属性在整个生命周期中都是变体,并且目标视图控制器需要多次“查询”源视图控制器,则委派可能会有更多吸引力。
Delegation is commonly used for allowing the delegated object to communicate back to the master object, either for asking for additional data or for performing callbacks. 委托通常用于允许委托对象与主对象进行通信,以请求其他数据或执行回调。
There's no reason in your case not to immediately provide the delegated object with everything it needs, since you have it available already, so the second option would be the most reasonable way to go. 由于您已经有了可用的对象,因此没有理由不立即向委托对象提供它所需的一切,因此第二种选择是最合理的选择。
Generally speaking, the delegation flow goes more or less like this 一般来说,委派流程或多或少像这样
Foo : "Hi Bar, here's a task for you. Use this data, and do your things"
Foo :“嗨,酒吧,这是您的任务。使用此数据来做您的事情”
[later on]
[稍后的]
Bar : "Hey boss, this thing just happened!"
酒吧 :“嘿老板,这件事刚发生!”
Foo : "Oh, nice! Thank you for letting me know"Foo :“哦,太好了!谢谢您让我知道”
[later on]
[稍后的]
Bar : "Hey boss, I'm gonna need something more to conclude the work"
酒吧 :“老板,我需要更多东西来完成工作”
Foo : "Here is is"Foo :“这里是”
[later on]
[稍后的]
Bar : "Hey boss, I'm done! Here's the result of my work"
酒吧 :“嘿老板,我完成了!这是我工作的结果”
The definition of a custom BazDelegate
protocol would be appropriate in case Baz
needs to communicate back to Foo
(say, the user performed a choice on the detailed view). 如果
Baz
需要与Foo
通信(例如,用户在详细视图上进行了选择),则自定义BazDelegate
协议的定义将是适当的。
In such case you would usually define something like 在这种情况下,您通常会定义如下内容
@protocol BazDelegate
- (void)baz:(Baz *)baz didFinishSelectingWhatever:(id)whatever;
@end
and then implement it in Foo
to receive meaningful callbacks from the delegated object. 然后在
Foo
实现它,以从委托对象接收有意义的回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.