swift - UITableViewController didSelectRowAt 未正确解除

标签 swift uitableview appdelegate dismissviewcontroller

正如你在我的代码中看到的,一旦选择了一行但没有成功,我尝试了一些方法将 UIViewTableController 关闭回我的父 ViewController

我的 UITableViewController 中的 didselectRowAt 代码:

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):

protocol ShowWeather{
    func showWeatherInfo()
}

我在 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()
}

显然,在取消打印之前和之后可以工作,但完成中的取消打印则不起作用。我目前还没有任何信息传递回 UIViewController 的原因是,我什至还没有到达那里。

是的,在我的 ViewController 类 header 中,我有 TableViewController 类协议(protocol),但这无关紧要,因为 UITableViewController 甚至还没有正确地关闭。

最佳答案

当您关闭 vc 时,它已被取消初始化,因此您需要在关闭之前传递数据

print("before dismissing \(delegate)")  // print delegate to see if nil
self.delegate?.showWeatherInfo()
self.dismiss(animated: true, completion: {
    print("dismissing")
})

此外,如果 vc 被推送到导航中 dismiss 将不起作用,您需要使用

self.navigationController?.popViewController(animated: true)

关于swift - UITableViewController didSelectRowAt 未正确解除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284334/

相关文章:

ios - 保留 Appdelegate

ios - 从 UIStackView 中缺少的代码添加 UITextView

ios - 为 iOS 构建 Telegram-Sdk

ios - UITableView 删除行减少表格行高

iphone - UITableViewCell : how to give the built-in imageView a margin on retina display

xcode - 使用 TabBar 和 NavBar 在 App Delegate 中配置窗口

ios - 应用程序 :openURL:sourceApplication:annotation: not found in iOS 8 的实现

ios - 根据出生日期计算年龄

ios - 如何在 Swift 中动态创建 buttonAction 函数

swift - 自定义UITableView给出NSInternalInconsistencyException