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!");
}
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!");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With