首页 > 解决方案 > 从视图控制器对 tableview 控制器进行 segue

问题描述

当以编程方式在我的视图控制器中点击一个按钮时,我试图对表视图控制器进行 segue。这是我的代码:

@objc func editProfileButtonAction(sender: UIButton!) {
    print("ButtonTapped")
    
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let identifier = segue.identifier {
        if identifier == "EditProfile" {
            var editProfileTableViewController = segue.destination as! EditProfileTableViewController
          editProfileTableViewController = self
        }
      }
    }
    
}

我真的可以使用一些帮助。我还需要使用同一视图控制器中的按钮对集合视图控制器进行 segue。

标签: swiftxcodetableviewsegue

解决方案


好的澄清一下。无法以编程方式创建 segue。Segue 是情节提要上的箭头,用于从一个 VC 链接到另一个 VC。它们被称为:performSegue。这调用了函数准备。

如果你想在点击按钮时显示一个新的 VC(没有 segue),那么你在按钮函数中使用 present(VC(), animated: true, completion: nil) }。VC 以模态方式呈现。

@objc func editProfileButtonAction(sender: UIButton!) {
    print("editProfileButtonAction")
    present(EditProfileTableViewController(), animated: true, completion: nil)
}

推荐阅读