解除在ViewController中以编程方式呈现的ViewController,它嵌套在NavigationController崩溃中,但不是每次都崩溃

时间:2016-12-03 14:57:36

标签: ios swift swift3 xcode8 ios10

当我试图解散一个ViewController时,它是在一个嵌套在NavigationController中的ViewController中以编程方式呈现的,我似乎得到了一个

libc ++ abi.dylib:以NSException类型的未捕获异常终止

每隔一段时间没有给出进一步的信息。

我的ViewController在嵌套的上面以模态方式呈现:

class RefreshController: UIViewController {

@IBOutlet var label: UILabel!
@IBOutlet var main: UIView!
@IBOutlet var spinner: UIActivityIndicatorView!



override func viewDidLoad() {
    super.viewDidLoad()

    spinner.startAnimating()
    label.text = .localized("refreshing") + " ..."

    main.layer.cornerRadius = 5
}

public func hideRefresh(_ contr: UIViewController, completion: @escaping (() -> Swift.Void)){
    contr.presentedViewController?.dismiss(animated: true, completion: {
        refreshController = nil
        completion()
    })
}

class func showRefresh(_ contr: UIViewController!, completion: @escaping (() -> Swift.Void)){
    refreshController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "refreshController") as! RefreshController

    contr.present(refreshController, animated: true, completion: {
        completion()
    })
}
}

我有一个全局变量refreshController,当它被显示为每次访问时,它将自己定义为当前呈现的控制器。

在嵌套的ViewController中,我调用:

refreshController.hideRefresh(self, completion: {
//code goes here, after the controller dismissed
}

(完成部分是由于动画,有时会与应该呈现的UIAlerts冲突)

有没有人有这方面的经验?或者它只是最新的iOS 10.2(14C82)测试版中的一个错误?

1 个答案:

答案 0 :(得分:0)

在呈现的viewController上调用dismiss,而不是在呈现的调用上:

public func hideRefresh(_ contr: UIViewController, completion: @escaping (() -> Swift.Void)){
    // instead of contr.presentedViewController?.dismiss...
    contr.dismiss(animated: true, completion: {
        refreshController = nil
        completion()
    })
}