gpt4 book ai didi

ios - 如何让 View Controller 在异步任务中保持事件状态?

转载 作者:行者123 更新时间:2023-11-28 22:37:44 26 4
gpt4 key购买 nike

我正在制作我的第一个 iPhone 应用程序并且有一点误解。

我有一个菜单 View Controller 和一个游戏 View Controller 。我希望我的菜单 View Controller 从可以传递到我的游戏 View Controller 的 Web 服务中异步获取一些天气数据。

在菜单到游戏之间的序列中,我的想法是这样的:

if user presses playgame BEFORE weather data loaded
store a reference to the game view Controller and send it the data later.
else
we already have the weather data so we'll just set send that now.

但是我没有意识到,一旦玩家转换到游戏 View Controller ,菜单 View Controller 中接收天气数据的函数就会停止触发!

有没有一种方法可以让一个 View Controller “事件”并在播放器位于另一个 View Controller 中时在后台运行?

最佳答案

因此假设您正在使用 nsurlconnection 来获取数据,您可以将连接的委托(delegate)设置为游戏 View Controller 并让它为您处理数据。

更理想的方法是将天气信息分离到一个不同的类中,该类扩展 nsobject 并实现 nsurlconnectiondelegate 协议(protocol),并将该类作为数据传递给您的游戏 View Controller ,因为理论上同一个类可以还包含所有天气数据。

关于ios - 如何让 View Controller 在异步任务中保持事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15439731/

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