Unexpectedly Found nil On TableView While Moving From ParentVC to ContainerVC

I have a problem with moving from parent viewcontroller to a container viewcontroller. My container viewcontroller has a UITableView inside it and whenever I try to switch to it from the parent viewcontroller it always return nil, saying that the UITableView doesn't exist.

I know that this problem is probably related to the UITableView is not available yet during the time when I were trying to move into the container viewcontroller. I try to move the setup code into ViewWillAppear but still it doesn't help so I assume this to be a kind of problem where the UITableView is not available yet to show on the screen. Below is my code:

    func showSearchAccountVC() {

        print("Begin to move into SearchAccountVC")

        let searchAccountVC = SearchAccountViewController()

        

        addChild(searchAccountVC)

        self.view.addSubview(searchAccountVC.view)

        

        searchAccountVC.didMove(toParent: self)

        searchAccountVC.view.frame = self.view.bounds

    }

// MARK: This is in container viewcontroller
class SearchAccountViewController: UIViewController {



    @IBOutlet weak var searchAccountTableView: UITableView!

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = .red

        // Do any additional setup after loading the view.

        //self.view.backgroundColor = .red

        searchAccountTableView.separatorStyle = .none

        searchAccountTableView.estimatedRowHeight = 100

        searchAccountTableView.rowHeight = UITableView.automaticDimension

        searchAccountTableView.delegate = self

        searchAccountTableView.dataSource = self

    }
}

Can someone tell me how would I solve this kind of problem? Your response will be highly appreciated!
When you post code it is better to use "Paste and Match Style"
and the code formatter tool (<>)

Code Block
func showSearchAccountVC() {
print("Begin to move into SearchAccountVC")
let searchAccountVC = SearchAccountViewController()
addChild(searchAccountVC)
self.view.addSubview(searchAccountVC.view)
searchAccountVC.didMove(toParent: self)
searchAccountVC.view.frame = self.view.bounds
}
// MARK: This is in container viewcontroller
class SearchAccountViewController: UIViewController {
@IBOutlet weak var searchAccountTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
searchAccountTableView.delegate = self // Do it earlier
searchAccountTableView.dataSource = self
self.view.backgroundColor = .red
searchAccountTableView.separatorStyle = .none
searchAccountTableView.estimatedRowHeight = 100
searchAccountTableView.rowHeight = UITableView.automaticDimension
// searchAccountTableView.delegate = self
// searchAccountTableView.dataSource = self
}
}

Which line is the crash ? line 24 ?
Did you connect searchAccountTableView to the tableview in Interface Builder (control-drag from the dot to the tableView)

I have a problem with moving from parent viewcontroller to a container viewcontroller. My container viewcontroller has a UITableView inside it

Who is parent ViewController ?
Who is the container viewcontroller ? Is it SearchAccountViewController ?

whenever I try to switch to it from the parent viewcontroller it always return nil, saying that the UITableView doesn't exist. 

How do you "switch"
Where is it in code ?

I would also advise to move delegate assignment sooner.
The worst part of your code is this line:
Code Block
let searchAccountVC = SearchAccountViewController()


Generally, instantiating a view controller with init() always a bad option in almost all cases.
If you want to make @IBOutlets or all other views constructed on the storyboard work, you need to instantiate a view controller through the storyboard.
Code Block
        guard let searchAccountVC = self.storyboard?.instantiateViewController(identifier: "SearchAccountViewController") as? SearchAccountViewController else {
            print("Something wrong in storyboard")
            return
        }

(You need to put Storyboard ID as SearchAccountViewController in your storyboard, when you want to use this code.)


One more, why don't you use present(_:animation:complete:) to show SearchAccountViewController?
Code Block
func showSearchAccountVC() {
print("Begin to move into SearchAccountVC")
guard let searchAccountVC = self.storyboard?.instantiateViewController(identifier: "SearchAccountViewController") as? SearchAccountViewController else {
print("Something wrong in storyboard")
return
}
searchAccountVC.modalPresentationStyle = .fullScreen //Change this as you like
self.present(searchAccountVC, animated: true, completion: nil)
}


If you really want to implement so called ContainerVC, you should be define a Container View on your storyboard.
With using Container View, you are not worried with instantiating a VC. You just need to hide and show your Container View.
@Claude31
  1. Yes, line 24 got crash when I try to switch from my parent viewcontroller.

  2. parent ViewController is SearchViewController while the child viewcontroller is SearchAccountViewController. I try to switch to the child ViewController whenever user tap on a UISearchBar.

  3. I switch using:

Code Block
class SearchViewController: UIViewController {
func showSearchAccountVC() {
print("Begin to move into SearchAccountVC")
let searchAccountVC = SearchAccountViewController()
addChild(searchAccountVC)
self.view.addSubview(searchAccountVC.view)
searchAccountVC.didMove(toParent: self)
searchAccountVC.view.frame = self.view.bounds
}
}

This is a delegate method from a UITableViewCell class which will trigger whenever user tap on a UISearchBar.

To sum up the problem, tableView is perfectly connected to the ViewController but whenever I try to switch to it from the parent ViewController, Xcode give me the "Unexpected Found nil" message saying that the tableviw is nil.

@OOPer

I guess I am thinking wrong on using container viewcontroller, I think I suppose show a new viewcontroller on top of another viewcontroller instead of using a container viewcontroller.

I think I suppose show a new viewcontroller on top of another viewcontroller instead of using a container viewcontroller.

Thanks for replying to my comment. Please tell us how you have made it when you successfully implement what you want.
Unexpectedly Found nil On TableView While Moving From ParentVC to ContainerVC
 
 
Q