我正在为 iOS 平台编写一款回合制游戏。客户端使用 CocoaAsyncSocket API 与远程服务器进行通信。现在我正在处理这种情况:客户端已经不活动一段时间了,并且由于超时而与服务器断开连接。如果是这样的话,我希望当应用程序进入前台时弹回登录 View ,让用户再次登录。
我假设我必须在应用程序委托(delegate)的 -(void)applicationWillEnterForeground 中完成此类工作。检查我是否已连接不是问题,但我不知道如何关闭每个呈现 View Controller 以带我返回 Root View (恰好是登录 View )。
我的 View Controller 以模态方式呈现,但我还有两个带有表格 View 的导航 Controller ,包括推送序列。
我们非常感谢任何有关此问题的帮助。提前致谢!
最佳答案
最不和谐的方法就是用新的登录 View Controller 替换 rootViewController。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if ([self isDisconnected]) {
self.window.rootViewController = [MyLoginController new];
}
}
对于 Storyboard,假设您的初始 Storyboard是登录 Storyboard。
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if ([self isDisconnected]) {
UIStoryboard *storyboard = self.window.rootViewController.storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
}
}
根据您的用户体验,这可能合适也可能不合适。
关于ios - 返回 Root View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21605205/