As you can see below in my code I've tried a few things to dismiss the UIViewTableController back into my parent ViewController once a row is selected to no luck
didselectRowAt code in my UITableViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//self.storyboard?.instantiateInitialViewController()
//self.navigationController?.popViewController(animated: true)
print("before dismissing")
self.dismiss(animated: true, completion: {
print("dismissing")
self.delegate?.showWeatherInfo()
})
print("after dismissing")
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(self.searchResults[indexPath.row], completionHandler: {(placemarks: [CLPlacemark]?, error: Error?) -> Void in
if let placemark = placemarks?[0] {
print(placemark)
}
})
/*
self.dismiss(animated: true, completion: {
self.delegate?.showWeatherInfo()
})
*/
}
protocol for that same class:
protocol ShowWeather{
func showWeatherInfo()
}
where I call it in my ViewController:
@IBAction func searchButton(_ sender: UIButton) {
searchButtonUIChanges()
textFieldBg_view.backgroundColor = UIColor.clear
//create searchresult object and add it as a subview
searchResultController = SearchResultsController()
searchResultController.delegate = self
addChild(searchResultController)
searchResultController.view.backgroundColor = UIColor.clear
searchResultController.view.frame = CGRect(x: 0, y: 64+textFieldBg_view.frame.size.height - 25, width: self.view.frame.size.width, height: self.view.frame.size.height - 50)
view.addSubview(searchResultController.view)
searchResultController.didMove(toParent: self)
let screenHeight = -(UIScreen.main.bounds.height/2 + textFieldBg_view.frame.height/2)
textFieldBg_view.transform = CGAffineTransform(translationX: 0, y: screenHeight)
UIView.animate(withDuration: animateDuration, delay: delay, usingSpringWithDamping: springWithDamping, initialSpringVelocity: 0, options: .allowUserInteraction, animations: {
self.textFieldBg_view.transform = .identity
}, completion: nil)
searchTextField.becomeFirstResponder()
}
Obviously before and after dismissing prints work but the dismissing one inside of the completion doesn't. The reason I currently don't have any info being passed back to the UIViewController yet is because, well, I haven't even gotten there yet.
And yes, in my ViewController class header I have the TableViewController class protocol, but that's irrelevant since the UITableViewController isn't even properly dismissing anyway yet.
When you dismiss the vc it's deinited so you need to pass the data before the dismiss
print("before dismissing \(delegate)") // print delegate to see if nil
self.delegate?.showWeatherInfo()
self.dismiss(animated: true, completion: {
print("dismissing")
})
Also if the vc is pushed inside a navigation dismiss
won't work and you need to use
self.navigationController?.popViewController(animated: true)