gpt4 book ai didi

ios - 模型和 Controller 之间的通信

转载 作者:可可西里 更新时间:2023-11-01 00:24:33 25 4
gpt4 key购买 nike

从许多不同的来源阅读后,我很困惑 View 和 Model 应该如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com