1

table view在一个全新的项目中有一个正常的,没有额外的代码。在我的故事板中,我有 2 个视图控制器和一个navigation controller嵌入到First Controller. 在First Controller我有一个带有按钮和标签的表格视图。我已经在情节提要中segue从按钮到第二个控制器。cell

我想知道的是我什么时候deinit会先被调用controller,而不是被调用。我设置了断点,但似乎没有任何效果。

当我segue回到它工作时second,因为是。但是我们需要做什么才能在第一个控制器中运行?我需要先从吗?还是我需要明确指定其他地方?firstsecond controllercontrollerpoppeddeinitpopcontrollerstacknil

请帮我理解背后的正确概念?请指导我正确的方向。

代码-:

import UIKit

class ViewController: UIViewController {

    // CELL DATA ARRAY

     var data = [0,0,0,0,0,0,0,0,0,0]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // INITIALIZE total tap ARRAY WITH INDEX 0
    }

    deinit {
        print("Deleted")
    }
}

// TABLE VIEW METHODS

extension ViewController:UITableViewDelegate,UITableViewDataSource{

    // NUMBER OF ROWS
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    // number of sections
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    // Cell for row

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // deque cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.customLabel.text = "Total taps : \(String(data[indexPath.row]))"
        // CALL BACK BUTTON
        return cell
    }

}

自定义单元格-:

import UIKit

class CustomCell: UITableViewCell {

    // Outlets

    @IBOutlet weak var customCount: UIButton!
    @IBOutlet weak var customLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    // Cell button action

    @IBAction func countTaps(_ sender: UIButton) {

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

第二控制器-:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    deinit {
        print("denit")
    }
}
4

1 回答 1

3

当您使用 push/present 视图控制器时,第一个视图控制器不会被释放并且您不会看到deinit被调用。视图控制器所需的内存量可以忽略不计,但是将其以与您离开它相同的状态存在于内存中的好处是显着的(例如,在您的示例中,如果您关闭第二个视图控制器以返回表视图控制器,它确保它仍然滚动到与之前完全相同的位置)。

事实上,系统维护了所有已呈现但尚未弹出/关闭的视图控制器的层次结构。请参阅查看控制器层次结构

于 2017-12-10T16:11:06.493 回答