Swift IOS保持视图控制器在弹出后在后台运行

时间:2017-04-06 16:34:24

标签: ios swift uiview viewcontroller navigationcontroller

我的应用包含两个视图。第一个是GMSMapView,第二个用于连接发送坐标的蓝牙设备。

连接蓝牙设备后,我使用委托将信息发送回地图视图并移动标记。为了在我以前使用segues的视图之间进行转换,这并没有停止蓝牙视图控制器,并且数据按照我希望的方式显示在地图视图中。

我遇到了重新启动地图视图的问题所以我决定使用导航控制器。现在我使用push segue进入第二个视图,然后弹出以返回到第一个视图的同一个实例。太棒了,真有效!我现在的问题是弹出第二个视图似乎完全阻止它像以前一样在后台运行。有没有办法让它像以前一样在后台运行?

我目前用于弹出第二个视图的是

self.navigationController?.popViewControllerAnimated(true)

任何想法都将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:2)

A popped view controller does not "stop running". It is returned to you, and if you don't retain it, it is completely destroyed.

If you don't want that to happen, retain it when it is returned. You are currently ignoring the returned view controller:

 self.navigationController?.popViewControllerAnimated(true)

Instead, keep a reference to it:

self.mySecondViewController = 
    self.navigationController?.popViewControllerAnimated(true)

Be warned, however, that this is a very unusual architecture. You will not be able to use the storyboard segue to push again, because it will push a different copy. It would be better to abandon your navigation controller architecture entirely, as it is completely unsuited to the idea of a view controller persisting after it is popped. If you want an architecture where two view controllers persist simultaneously, you would be better off using a UITabBarController — or, even better, reorganize your app completely. The notion that you need the view controller to persist after being popped is a "bad smell": it means that you have put the functionality in the wrong place. Put the functionality in a place that does persist, rather than forcing the view controller to persist in some artificial way.