【问题标题】:Make a segue to a tableview controller from a view controller从视图控制器对 tableview 控制器进行 segue
【发布时间】:2020-08-03 18:58:12
【问题描述】:

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

@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
        }
      }
    }
    
}

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

【问题讨论】:

  • 这不是转场。还有什么问题?
  • 我明白了。我不知道如何编码。
  • 您必须在Interface Builder中设计segue,正确设置标识符并调用performSegue。如果需要传数据实现prepare(for segue)
  • 我可以看一个例子吗?
  • 这里有几个选项:stackoverflow.com/questions/5210535/…

标签: swift xcode tableview segue


【解决方案1】:

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

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

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

【讨论】:

  • 嘿,我很感激!我现在收到一个错误,即为我的用户个人资料图像打开一个 nil 的 UIImage,这是我在当前函数之前从未得到的。
  • 所以现在如果图像没有加载到EditProfileTableViewController.viewDidLoad() 中。然后你必须将它从 FristVC 传递给 SecondVC,像:let editProfileTableVC = EditProfileTableViewController()editProfileTableVC.profileImage = UIImage(named: String)present(editProfileTableVC, animated: true, completion: nil) 这样你就可以将数据设置到新的 VC。 var profileImage: UIImage!必须在 EditProfileTableViewController 类中创建。
  • 我有一个函数在 viewDidLoad() 中加载配置文件图像。我不确定出了什么问题。
  • 把你的 viewController 发给我,我去看看。我的商城可以在这个链接的支持下找到。 link
【解决方案2】:

确保 Storyboard 中的 segue 具有完全相同的标识符:“EditProfile”。通常我会在开头写小写的标识符。您还需要为 Segue 做准备。例如设置委托:

  // Set ViewController class as the delegate of the EditProfileTableViewControllerDelegate protocol
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let identifier = segue.identifier {
      if identifier == "EditProfile" {
        let editProfileTableViewController = segue.destination as! EditProfileTableViewController
        editProfileTableViewController = self
      }
    }
  }

在我编码的时候,我删除了所有的故事板,因为我几乎无法解决太多错误。现在我正在以编程方式完成这一切。起初,自己设置所有视图有点困难,但毕竟我很高兴我不再使用故事板。对于一些我需要xib的东西,以及测试故事板。如果您有兴趣:大多数 iOS 程序员都在使用故事板,所以如果您继续这样做也没关系。 以编程方式完成所有操作的优点是不再有 segue。所以就出现了,在导航 VC 的 push、pop、...

【讨论】:

  • 我收到错误,无法将类型“NewNetworkProfilesViewController”的值分配给类型“EditProfileTableViewController”
  • 看起来您的 Segue 指向 NewNetworkProfilesViewController,但您尝试访问 EditProfileTableViewController。确保您使用的是唯一标识符。此外,您应该为您在情节提要上创建的每个 ViewController 提供一个自己的类。就像使用类名创建一个新文件:NewNetworkProfilesViewController.swift 和 EditProfileTableViewController.swift。然后转到故事板并将类分配给您的 ViewControllers。那么你应该使用 as! EditProfileTableViewController 而不是“as!UITableViewController”。并在变量名中写下小写字母。让编辑
  • 我不知道如何以编程方式制作 segue。我之前更改了代码,应该已经更新了。我现在将发布新的编辑。
  • 我不能。这个视图控制器是以编程方式创建的。我没有这个 segue 的故事板。
  • 我所有的功能现在都是红色的,表示没有自我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2011-10-05
  • 2013-05-26
相关资源
最近更新 更多