Answer the question
In order to leave comments, you need to log in
MVC in IOS: Can a Model have a Delegate to a Controller?
As we can see in the picture the View can have a Delegate and a DataSource to the controller.
And I had a question, can a Model have a Delegate to a Controller?
Answer the question
In order to leave comments, you need to log in
You can assign any other object to any object as a delegate, this does not violate any other patterns, etc.
//файл A.h
@protocol ADelegate <NSObject>
- (void)aDelegateMethod;
@end
@interface A : NSObject
@property (nonatomic, weak) id<ADelegate> delegate;
/*
Описывая делегат таким образом мы скрываем объект за протоколом ADelegate.
В вашем случае модель не будет знать что у неё делегат это контроллер, модель будет
думать что там есть некий объект который отвечает на ADelegate протокол и кто там конкретно не важно
*/
@end
//файл B.h
#import "A.h"
@interface B : NSObject <ADelegate>
@end
//файл B.m
@implementation B
- (void)foo
{
A *a = [A new];
a.delegate = self;
}
#pragma mark - ADelegate Protocol
- (void)aDelegateMethod
{
}
@end
Maybe you can write whatever you want. Only in the picture that you posted, it says Model - KVO & Notification, and it's better to use them when updating data in the model, because Usually, several Controllers need to know about updating data, it’s easier to do this through notification, and Key-Value Observing is needed when one controller monitors some variable.
Usually the Controller has direct access to the Model. Or what are you talking about?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question