保持任务在视图控制器之间运行

时间:2018-02-20 07:36:06

标签: ios swift

我在使用Swift iOS项目的场景中寻找一些提示。

所以我有一个Modal view controller MKMapView,我需要运行这个Modal view controller,而它不是主要的观看视图控制器,它被解雇了。 我如何保持运行和更新,而不是主要观看view controller

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

您只需要为模态视图控制器保留strong reference, 小心只将它实例化一次,并在每次显示它时检查它是否存在。

示例:

import UIKit

class ViewController: UIViewController {
    var mapVc:MapViewController? // strong reference here

    @IBAction func showMapVc() {
        if self.mapVc == nil {
            self.mapVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MapVC") as? MapViewController
        }
        self.present(self.mapVc!, animated: true, completion: nil)
    }
}

class MapViewController: UIViewController {
    @IBAction func closeVc() {
        self.dismiss(animated: true, completion: nil)
    }
}