在某些事件上重置应用程序
我正在编写一个 iPhone 应用程序,在发生某个事件时,例如用户赢得游戏,我想在应用程序启动后立即将其重置回其初始状态。例如,再次执行 viewDidLoad() 等。在 Xcode 和 Objective C 中是否有简单的方法来执行此操作?谢谢。
I'm writing an iPhone app, and upon a certain event, say, the user winning the game, I would like to reset the application back to its initial state right after it is launched. For example, executing viewDidLoad() again, etc. Is there a simple way to do this in Xcode and objective C? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您负责应用程序的状态 - 如果您知道初始设置是什么,您应该能够将所有内容设置回该状态。没有简单的
[UIApplication重置]
方法,但是有一些技术可以使工作变得更容易:当您想要重置应用程序时发布通知,并确保任何可能的对象需要重置自身(例如视图控制器)侦听该通知。
将初始化代码移至不同的方法中,然后从
-init
方法和重置通知处理程序中调用它。您可以通过将视图控制器的 view 属性设置为 nil 来重新加载其视图。下次访问该属性时,将调用
-loadView
,然后将加载相关的 .xib 或创建必要的视图。在许多情况下,您也许只需将应用的数据模型重置为初始状态即可。应用程序中的视图通常应该反映数据模型的状态,因此清除数据应该会导致视图也返回到其初始状态。
考虑一下为什么你真的需要这个。如果您在某处有“重置为出厂默认设置”按钮,这可能是有意义的,但这也可能表明需要更多设计。为什么用户不能选择所有数据并将其删除?您持有哪种状态,用户可能希望以这种方式重置?
You're in charge of the state of your app -- if you know what the initial settings are, you should be able to set everything back to that state. There's no simple
[UIApplication reset]
method, but there are a few techniques that make the job easier:Post a notification when you want to reset the app, and make sure that any objects that might need to reset themselves (such as view controllers) listen for that notification.
Move your initialization code into a different method, and then call it from both your
-init
method and your reset notification handler.You can get a view controller to reload its view by setting its view property to nil. The next time that property is accessed,
-loadView
will be called, and that in turn will load the relevant .xib or create the necessary views.In many cases, you may be able to just reset the app's data model to an initial state. The views in your app should typically reflect the state of the data model, so clearing the data should cause the views to go back to their initial state as well.
Consider why you really need this. It might make sense if you have a "reset to factory defaults" button somewhere, but it might also be an indication that more design is required. Why can't the user just select all the data and delete it? What state are you holding onto that the user might want to reset in this way?