如果我在另一个视图上添加子视图而不删除它,则该应用会被挂起

时间:2018-07-26 09:44:44

标签: ios swift uiviewcontroller

如果我尝试在另一个添加为子视图的视图控制器上添加子视图,我的应用程序将被挂起。

有什么方法可以获取背景视图中的视图控制器?这样我就可以删除视图背景中的视图控制器。

Image 1

Image 2

let viewController = UIStoryboard(name: "Common", bundle: nil).instantiateViewController(withIdentifier: "ContactOptionsDialouge") as! ContactOptionsDialouge
viewController.initializeDataBeforePresentingView(presentConatctUserBasicInfo : userBasicInfo,supportCall: checkAndEnableCallOption(callSupport: matchedUser!.callSupport), delegate: nil, isRideStarted: isRideStarted!)
self.navigationController?.view.addSubview(viewController.view)
self.navigationController?.addChildViewController(viewController)

这是我用来将视图控制器添加为子视图的代码。另一个顶视图控制器在其他用户使用mqtt连接接受骑行时自动提供

2 个答案:

答案 0 :(得分:1)

似乎您的代码并非没有崩溃。如果相关,可以使用以下代码:

guard let viewController = UIStoryboard(name: "Common", bundle: nil).instantiateViewController(withIdentifier: "ContactOptionsDialouge") as? ContactOptionsDialouge else { print("viewController is nil"); return; }
guard let matchedUser = self.matchedUser else { print("matchedUser is nil"); return; }
guard let isRideStarted = self.isRideStarted else { print("isRideStarted is nil"); return; }

viewController.initializeDataBeforePresentingView(presentConatctUserBasicInfo: userBasicInfo, supportCall:  checkAndEnableCallOption(callSupport: matchedUser.callSupport), delegate: nil, isRideStarted: isRideStarted)
self.view.addSubview(viewController.view)
self.addChildViewController(viewController)

答案 1 :(得分:0)

self.navigationController?.view.addSubview(viewController.view)
self.navigationController?.addChildViewController(viewController)

添加下面的代码行而不是上面的代码对我来说很好:-

self.view.addSubview(viewController.view)
self.addChildViewController(viewController)