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!
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!