【发布时间】:2018-05-24 05:46:45
【问题描述】:
我在一个全新的项目中有一个普通的table view,没有额外的代码。在我的故事板中,我有 2 个视图控制器,以及一个嵌入到 First Controller 的 navigation controller 。在First Controller 我有一个带有按钮和标签的表格视图。我已将 segue 从 cell 按钮提供给情节提要中的第二个控制器。
我想知道我的deinit 何时会被第一个controller 调用,它不会被调用。我设置了断点,但似乎没有任何效果。
当我 segue 从 second 回到 first 时,它适用于 second controller,因为 controller 是 popped。但是我们需要做什么才能在第一个控制器中运行deinit?我是否需要先pop controller 以及stack?还是我需要在其他地方明确指定nil?
请帮我理解背后的正确概念?请指导我正确的方向。
代码-:
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")
}
}
【问题讨论】:
-
您没有展示第一个视图控制器是如何创建的。为什么要让第一个视图控制器消失;应用消失后会留下什么?
-
@meaning-matters 我知道什么都不会留下,但我只是在检查测试。这可能吗?