Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 关闭视图控制器后未调用OSX deinit_Swift_Macos_Memory Leaks_Deinit - Fatal编程技术网

Swift 关闭视图控制器后未调用OSX deinit

Swift 关闭视图控制器后未调用OSX deinit,swift,macos,memory-leaks,deinit,Swift,Macos,Memory Leaks,Deinit,在“deinit”方法中,存在断点未命中的主题。解决方案是在里面放一个可执行代码。试过了-没用 从第一个窗口启动ViewController的代码: let vc = self.storyboard?.instantiateController(withIdentifier: "testwindow") as! NSViewController self.presentViewControllerAsModalWindow(vc) 它打开一个新窗口,其中有一个按钮,单击该按钮可

在“deinit”方法中,存在断点未命中的主题。解决方案是在里面放一个可执行代码。试过了-没用

从第一个窗口启动
ViewController
的代码:

    let vc = self.storyboard?.instantiateController(withIdentifier: "testwindow") as! NSViewController
    self.presentViewControllerAsModalWindow(vc)
它打开一个新窗口,其中有一个按钮,单击该按钮可调用以下代码

    dismissViewController(self)
这是带有断点的Denit代码

方案中未启用僵尸对象

如果在弹出窗口再次出现时重新使用viewcontroller,则不会出现问题,但是每次都会创建新的视图控制器实例


要确保对象被销毁,需要做些什么吗?

只需尝试复制以下代码,这些代码与您在每个viewController中使用的按钮相同,并成功命中断点,同时在deInit中将var num从0(viewDidLoad)更改为1并返回到0:

你在尝试什么?当您点击Disclease时,它不是在调用Denit方法吗

//主视图控制器

@IBAction func letsGo(_ sender: UIButton) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "second") as? SecondViewController {
            self.present(vc, animated: true, completion: nil)
        }
    }
class SecondViewController: UIViewController {

    var num = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        num += 1
        print("\(num)")
    }

    @IBAction func dismissTheHype(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

    deinit {
        print("deiniting")

        num -= 1
        doNothing()
        print("printing number \(num)")
    }

    func doNothing(){
        var number = 4
        number += 1
        print("\(number) times")
    }
}
//第二视图控制器

@IBAction func letsGo(_ sender: UIButton) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "second") as? SecondViewController {
            self.present(vc, animated: true, completion: nil)
        }
    }
class SecondViewController: UIViewController {

    var num = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        num += 1
        print("\(num)")
    }

    @IBAction func dismissTheHype(_ sender: UIButton) {
        self.dismiss(animated: true, completion: nil)
    }

    deinit {
        print("deiniting")

        num -= 1
        doNothing()
        print("printing number \(num)")
    }

    func doNothing(){
        var number = 4
        number += 1
        print("\(number) times")
    }
}

苹果公司关于解除使用
presentViewControlleramodalWindow()
显示的控制器的文档说明“要解除模式窗口,请调用self(呈现视图控制器)上的dismissViewController(:)方法。”。因此,您可能会从呈现的控制器本身中解除呈现的控制器。从presenting view controller调用Disclose将有所帮助。

我想说,您仍然有一个或多个指向您的VC的强引用。这样,在调用Disclease时它不会被取消初始化。@dvdblk除了我提到的代码外,我没有任何额外的代码。这是一个测试项目。故事板是否可能以某种方式阻止它被发布?@Ruzard调用dismissViewController(self)位于哪个类?视图或显示视图的演示者。该方法的文档说明“要关闭模式窗口,请在self(显示视图控制器)上调用dismissViewController(u:)方法。”。这可能是您的代码中的一个潜在问题。@RohanBhale显然我漏掉了那一行。你说得对。非常感谢。如果您可以将其作为一个单独的答案发布-我会将其标记为解决方案并给予奖励。@Ruzard请检查我的答案。我尝试了您的代码-是的,它确实按预期工作。然而,我不是为iOS做这件事的。我想这是架构上的差异。顺便说一下,OP使用的是
PresentViewControlleraModalWindow
,也就是说,它是模态的。