N
N
Ne0nX2014-02-02 01:09:47
Objective-C
Ne0nX, 2014-02-02 01:09:47

MVC in IOS: Can a Model have a Delegate to a Controller?

062a.png
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

4 answer(s)
D
Denis Morozov, 2014-03-14
@Ne0nX

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

A
Alexander, 2014-02-02
@alexyat

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.

S
SevaUA, 2014-02-02
@SevaUA

Usually the Controller has direct access to the Model. Or what are you talking about?

R
Roman, 2014-02-02
@firmach

As an example - CLLocationManager and its delegate. If I understand the question correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question