I have a normal table view
in a completely new project , with no extra code. In my storyboard I have 2 view controllers , and a navigation controller
embedded to First Controller
. In First Controller
I have a table view with button and label. I have given segue
from cell
button to second controller in storyboard.
What I want to know is when my deinit
will be called for first controller
, it is not getting called. I set break point and nothing seems to be working.
When I segue
back from second
to first
it works for second controller
, because controller
is popped
. But what we need to do for running deinit
in first controller? Do I need to pop
first controller
as well from stack
? Or do I need to explicitly specify nil
somewhere else ?
Please help me understand the correct concept behind? Please guide me to correct direction on this.
Code-:
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
}
}
Custom 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
}
}
Second Controller-:
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")
}
}
When you use push/present a view controller, the first view controller is not deallocated and you will not see
deinit
called. The amount of memory required by view controllers is negligible, but the benefit of having it there in memory, in the same state that you left it, is significant (e.g. in your example, if you dismiss the second view controller to return to the table view controller, it ensures that it's still scrolled to the exact same place it was before).In fact, the system maintains a hierarchy of all of the view controllers that have been presented, but have not yet been popped/dismissed. See View Controller Hierarchy.