从许多不同的来源阅读后,我对 如何在 MVC 中与 Swift 进行通信感到非常困惑

如何用 Swift 做同样的事情(在 Objective-c 中)

在模型中:

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

处理 View Controller 中的“ReceivedData”通知:
(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!");
}

关于ios - 模型和 Controller 之间的通信,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24831994/

10-16 21:10