Ios 关闭我的视图控制器,但;准备;下面的视图控制器

Ios 关闭我的视图控制器,但;准备;下面的视图控制器,ios,swift,xcode,uitableview,Ios,Swift,Xcode,Uitableview,我有一个modally-presentedViewController,里面有一个UITableView。当用户单击“天”时,它会显示此视图控制器(模式)。这是我的故事板: 我想做的是,当我在模式中单击表视图中的一个单元格时,我想取消模式,但也要在根视图控制器中加载新数据 如何从分段视图控制器访问根视图控制器 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self

我有一个modally-presented
ViewController
,里面有一个
UITableView
。当用户单击“天”时,它会显示此视图控制器(模式)。这是我的故事板:

我想做的是,当我在模式中单击表视图中的一个单元格时,我想取消模式,但也要在根视图控制器中加载新数据

如何从分段视图控制器访问根视图控制器

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.dismiss(animated: true, completion: nil)
}

// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! DayView
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.day = days![indexPath.row]
    }
}

我想要类似于我上面所写的东西,但虽然我的模式确实会被取消,但我想要的是,当单击TableViewCell时,prepare函数中的代码会以某种方式触发。

比协议/委托更短、更简单的方法是创建闭包:

要返回字符串,请在第一个ViewController中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewControllerB = segue.destination as? ViewControllerB {
        viewControllerB.callback = { message in
            
            //Do what you want in here!

        }
    }
}
在ViewControllerB中:


var callback : ((String) -> Void)?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    callback?("Your Data")
    self.dismiss(animated: true, completion: nil)
}


比协议/委托更短、更简单的方法是创建闭包:

要返回字符串,请在第一个ViewController中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewControllerB = segue.destination as? ViewControllerB {
        viewControllerB.callback = { message in
            
            //Do what you want in here!

        }
    }
}
在ViewControllerB中:


var callback : ((String) -> Void)?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    callback?("Your Data")
    self.dismiss(animated: true, completion: nil)
}


您的路径是正确的,您需要再添加两个函数1。其中一个不是目的地,而是它的源,所以您需要从目的地更改为源

 // MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.source as! DayView
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.day = days![indexPath.row]
    }
}

供参考:

您的路径正确,需要再添加两个函数1。其中一个不是目的地,而是它的源,所以您需要从目的地更改为源

 // MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.source as! DayView
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.day = days![indexPath.row]
    }
}
供参考: