ios – Trying to understand an error caused by UITableView didSelectRow function


I have set the tableView didSelectRowAt function for my TableView. It leads to another “Detail”-ViewController. When I click on a Row my App crashes and I get this error: ” Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value” at my viewDidLoad() function in my First ViewController (where the TableView is).

I found out that it has to do with my IBOutlets since the error always occurs at the first assignment of an IBOutlet that is declared as an implicitly unwrapped optional. I was able to fix it by making all IBOutlets optional that appear in my viewDidLoad function but I don’t think it’s intended to work like this, or is it?

I should also note that there is another ViewController where you set the content of the row. So you go from ViewController1 to ViewController2, set up the Data for the TableView Rows content, press a Button and get back via a Segue. And then you can click on that Row to get to the Detail View.

So my question is if it’s the right way to prevent this error by making my IBOutlets optional when reassigning them or if this could cause some problems later on. Why is it even loading my ViewDidLoad function when I select a row? I’ve read a bit about life cycles but I’m not sure how I’d need to set it up the right way. Probably a noob question but I’ve only started with Swift 2 weeks ago. Here is my Code with some comments to understand it better:

ViewController1 (Main VC)

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SendingBookDataProtocol {

    @IBOutlet weak var headline: UILabel?  // not causing error since declared as an optional
    @IBOutlet weak var addBookButton: UIButton! // returns nil when didSelectRow function is executed
    @IBOutlet var tableView: UITableView!

 override func viewDidLoad() {
        // Do any additional setup after loading the view.
        print("Print this:", addBookButton)
        headline?.text = "Deine \nBibliothek"
        headline?.numberOfLines = 0
        tableView?.delegate = self
        tableView?.dataSource = self
        let nib = UINib(nibName: "BookCell", bundle: nil)
        tableView?.register(nib, forCellReuseIdentifier: "BookCell")
        addBookButton.layer.cornerRadius = addBookButton.frame.width / 2  // Fatal Error: Unexpectedly found nil...
        addBookButton.layer.masksToBounds = true

// my didSelectRowAt function

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      if let bookDetailVc = storyboard?.instantiateViewController(withIdentifier: "BookDetailView") as? BookDetailViewController{
          self.navigationController?.pushViewController(bookDetailVc, animated: true)

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "getBookData" {
                let addBookVC: AddBookController = segue.destination as! AddBookController
                addBookVC.delegate = self