Avoid creation of multiple ViewController instances, iOS Swift(避免创建多个 ViewController 实例,iOS Swift)
问题描述
在 AppDelegate 中,每当收到 VoIP 呼叫(推送通知)时,都会调用以下函数,从而创建多个VideoCallViewController"实例
In AppDelegate, the below function gets called whenever VoIP call (push notification) is received thereby creating multiple instances of "VideoCallViewController"
如下所示,我使用 deinit(在 VideoCallViewController 中)来检查是否在创建新的VideoCallViewController"实例之前取消了先前的VideoCallViewController"实例,令我惊讶的是 print("Deinitializing VC) 未被调用,将实例留在内存中.
I've used deinit (in VideoCallViewController) as shown below, to check if the previous instance of "VideoCallViewController" was being de-initialised before a new instance of "VideoCallViewController" is created, to my surprise print("Deinitializing VC) wasn't called, leaving the instance in memory.
如果 AppDelegate 中已经存在 VideoCallViewController 的实例,我如何显示 VideoCallViewController 及其导航控制器.
How can I show VideoCallViewController with its Navigation Controller if an instance of VideoCallViewController already exists from AppDelegate.
在 VideoCallViewController 中
deinit {
print("Deinitializing VC)
}
在 AppDelegate 中
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
videoVC = storyboard.instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = videoVC
self.window?.makeKeyAndVisible()
}
推荐答案
可能的解决方案:创建单例持有对可空窗口的引用,负责视频通话流程.在通知更改可见窗口.通话结束 - 返回应用程序主窗口.
Possible solution: Create singleton holding reference to nullable window, responsible for video calls flow. On notification change visible windows. On call end - return to the application main window.
例如:
class VideoCallManager {
//MARK: - Singleton
static let sharedInstance = VideoCallManager()
private init() {}
private var videoCallWindow: UIWindow?
func navigateToVideoCallViewController() {
if let window = self.videoCallWindow, window.keyWindow {
//VideoCallViewController is displayed at the moment.
return
}
videoCallWindow = UIWindow.init(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
videoVC = storyboard.instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController
self.videoCallWindow?.rootViewController = videoVC
self.videoCallWindow?.makeKeyAndVisible()
}
func returnToWindowOfAppDelegate() {
if let window = self.videoCallWindow, window.keyWindow {
(UIApplication.sharedApplication().delegate as? AppDelegate)?.window?.makeKeyAndVisible()
self.videoCallWindow = nil
}
}
}
你的方法看起来像:
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
VideoCallManager.sharedInstance.navigateToVideoCallViewController()
}
这篇关于避免创建多个 ViewController 实例,iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:避免创建多个 ViewController 实例,iOS Swift
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
