Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between Model and Controller

After reading from many different sources, I am very confused about how View and Model should communicate in the MVC with Swift

How to do the same thing with Swift (here in objective-c)

In the model:

(void)receivedMessageFromServer {
    // Fire the notification
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedData" object:nil];   
} 

Handle the "ReceivedData" notification in View Controller(s):

(void)viewDidLoad {
    [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedDataNotification:) name:@"ReceivedData" object:nil];
}

-(void)receivedDataNotification:(id)object {
    NSLog(@"Received Data!");
}
like image 772
FREDERIC1405 Avatar asked Sep 22 '26 00:09

FREDERIC1405


1 Answers

func receivedMessageFromServer() {
    NSNotificationCenter.defaultCenter().postNotificationName("ReceivedData", object: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedDataNotification:", name: "ReceivedData", object: nil)
}

func receivedDataNotification(object: AnyObject) {
    println("Received Data!");
}
like image 174
Keenle Avatar answered Sep 24 '26 21:09

Keenle