Table 查看未调用 deinit
Table view deinit not called
我在一个全新的项目中有一个正常的 table view
,没有额外的代码。在我的故事板中,我有 2 个视图控制器和一个嵌入到 First Controller
的 navigation controller
。在 First Controller
中,我有一个带有按钮和标签的 table 视图。我已将 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")
}
}
当您使用 push/present 视图控制器时,第一个视图控制器不会被释放,您将不会看到 deinit
被调用。视图控制器所需的内存量可以忽略不计,但是将它保存在内存中的好处与您离开它时的状态相同,这是很重要的(例如,在您的示例中,如果您将第二个视图控制器关闭到 return 到 table 视图控制器,它确保它仍然滚动到与之前完全相同的位置。
事实上,系统维护了所有已呈现但尚未 popped/dismissed 的视图控制器的层次结构。参见 View Controller Hierarchy。
我在一个全新的项目中有一个正常的 table view
,没有额外的代码。在我的故事板中,我有 2 个视图控制器和一个嵌入到 First Controller
的 navigation controller
。在 First Controller
中,我有一个带有按钮和标签的 table 视图。我已将 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")
}
}
当您使用 push/present 视图控制器时,第一个视图控制器不会被释放,您将不会看到 deinit
被调用。视图控制器所需的内存量可以忽略不计,但是将它保存在内存中的好处与您离开它时的状态相同,这是很重要的(例如,在您的示例中,如果您将第二个视图控制器关闭到 return 到 table 视图控制器,它确保它仍然滚动到与之前完全相同的位置。
事实上,系统维护了所有已呈现但尚未 popped/dismissed 的视图控制器的层次结构。参见 View Controller Hierarchy。