简体   繁体   English

正确地从 tableviewcell 内的 collectionview 转到另一个 viewcontroller

[英]Properly segue from collectionview inside tableviewcell to another viewcontroller

I am trying to segue (pass data) from a collectionview inside a tableviewcell to a another viewcontroller.我正在尝试将(传递数据)从 tableviewcell 内的 collectionview 转移到另一个 viewcontroller。 I tried using the didselect delegate but stuck on how to pass the data properly.我尝试使用 didselect 委托,但坚持如何正确传递数据。 It seems i kind of somehow hack my way around it but i would like to learn the proper way.似乎我以某种方式绕过它,但我想学习正确的方法。 Below is my code:下面是我的代码:

My Main view controller:我的主视图控制器:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue" {
        let vc = segue.destination as! DetailViewController
        vc.text = "Hello World"
    }
}

func segue() {
    self.performSegue(withIdentifier: "segue", sender: self)
}

}

My Table View:我的表视图:

 import UIKit

 class MainTableView: UITableView, UITableViewDelegate, UITableViewDataSource {

override func awakeFromNib() {
    self.delegate = self
    self.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell", for: indexPath) as! MainTableViewCell

    return cell
}

}

My Collection View:我的收藏视图:

import UIKit

class MoviesCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {

override func awakeFromNib() {
    self.delegate = self
    self.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MoviesCollectionViewCell", for: indexPath) as! MoviesCollectionViewCell

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let vc = ViewController()
    vc.segue()
}

}

The View controller I am trying to segue to:我正在尝试使用的视图控制器:

class DetailViewController: UIViewController {

@IBOutlet weak var label: UILabel!

var text: String?

override func viewDidLoad() {
    super.viewDidLoad()

    self.label.text = text
    // Do any additional setup after loading the view.
}
}

My tableviewcell and collectionviewcell are empty at the moment.我的 tableviewcell 和 collectionviewcell 目前是空的。

Your question is a little hard to answer as currently stated because it's not entirely clear what you are doing.像目前所说的那样,您的问题有点难以回答,因为尚不清楚您在做什么。 For example, it's not clear if you are using Storyboards or not.例如,不清楚您是否使用故事板。

If you are, then you probably want to define your segues in the storyboard and let UIKit invoke them for you.如果是,那么您可能想在故事板中定义您的转场,并让 UIKit 为您调用它们。 This documentation provides the overview that will hopefully help you.文档提供了希望对您有所帮助的概述。 In particular, you don't have to do the segue manually because UIKit will do it for you once you've set it up in the Storyboard (emphasis mine):特别是,您不必手动执行 segue,因为一旦您在 Storyboard 中设置了它(重点是我的),UIKit 就会为您执行此操作:

You do not need to trigger segues programmatically.您不需要以编程方式触发 segue。 At runtime, UIKit loads the segues associated with a view controller and connects them to the corresponding elements.在运行时,UIKit 加载与视图控制器关联的 segue 并将它们连接到相应的元素。 When the user interacts with the element, UIKit loads the appropriate view controller, notifies your app that the segue is about to occur, and executes the transition.当用户与元素交互时,UIKit 加载适当的视图控制器,通知您的应用程序即将发生转场,并执行转换。 You can use the notifications sent by UIKit to pass data to the new view controller or prevent the segue from happening altogether.您可以使用 UIKit 发送的通知将数据传递给新的视图控制器,或者完全防止 segue 发生。

In figure 9-4 you will see the event flow of a segue process.在图 9-4 中,您将看到 segue 进程的事件流。 In particular, note that if you override prepareForSegue:sender: in your source view controller for the segue then that is your opportunity to prepare data and send it to the destination view controller (either view setting the representedObject for the destination view controller, or via a custom setter method defined for your destination view controller class).特别要注意的是,如果您在源视图控制器中为 segue 覆盖prepareForSegue:sender:那么这是您准备数据并将其发送到目标视图控制器的机会(视图设置目标视图控制器的representedObject对象,或通过为目标视图控制器类定义的自定义 setter 方法)。 The description text below that figure spells it out:该图下方的描述文字说明了这一点:

The prepareForSegue:sender: method of the source view controller lets you pass data from the source view controller to the destination view controller.源视图控制器的prepareForSegue:sender:方法允许您将数据从源视图控制器传递到目标视图控制器。 The UIStoryboardSegue object passed to the method contains a reference to the destination view controller along with other segue-related information.传递给该方法的 UIStoryboardSegue 对象包含对目标视图控制器的引用以及其他与 segue 相关的信息。

If you aren't using storyboards, then you're essentially doing the same thing by hand.如果您不使用故事板,那么您实际上是在手动做同样的事情。 You allocate the UIViewController subclass you want to show in response to the touch in the cell item, and then set its representedObject to the data it should display (or call a custom method defined by that class and pass in the data the view controller needs to display), then you show the view controller.您分配要显示的 UIViewController 子类以响应单元格项中的触摸,然后将其representedObject设置为它应该显示的数据(或调用由该类定义的自定义方法并传入视图控制器需要的数据显示),然后显示视图控制器。 To present it manually you'll want to read the Presenting a View Controller document from Apple.要手动呈现它,您需要阅读 Apple 的Presenting a View Controller文档。 In particular:特别是:

Presenting a View Controller呈现一个视图控制器

There are several ways to initiate the presentation of a view controller:有几种方法可以启动视图控制器的呈现:

Use a segue to present the view controller automatically.使用 segue 自动呈现视图控制器。 The segue instantiates and presents the view controller using the information you specified in Interface Builder. segue 使用您在 Interface Builder 中指定的信息实例化并呈现视图控制器。 For more information on how to configure segues, see Using Segues .有关如何配置 Segue 的更多信息,请参阅使用 Segue Use the showViewController:sender: or showDetailViewController:sender: method to display the view controller.使用showViewController:sender:showDetailViewController:sender:方法来显示视图控制器。 In custom view controllers, you can change the behavior of these methods to something more suitable for your view controller.在自定义视图控制器中,您可以将这些方法的行为更改为更适合您的视图控制器的行为。 Call the presentViewController:animated:completion: method to present the view controller modally.调用presentViewController:animated:completion:方法以模态呈现视图控制器。

Hopefully that's enough to get you going.希望这足以让你继续前进。 If not, maybe clarify your question with a bit more context and we'll try again.如果没有,也许可以用更多的上下文来澄清您的问题,我们会再试一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用segue从tableViewCell跳到另一个viewController - Not able to jump from tableViewCell to another viewController using segue 如何从自定义 TableViewCell 转到另一个 ViewController (Swift) - How to Segue from Custom TableViewCell to Another ViewController (Swift) 在一个标签栏控制器中从一个ViewController切换到另一个ViewController? - Segue from one ViewController to another ViewController inside a Tab Bar Controller? 从CollectionView到ViewController隔离项目信息 - Segue item info from CollectionView to ViewController 从 TableViewCell 内部 TableViewCell go 到 viewcontroller - from a TableViewCell Inside a TableViewCell go to a viewcontroller 当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController - how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell 从UITableViewCell到另一个ViewController - Segue from UITableViewCell to another ViewController 从TableViewCell NIB到ViewController的Segue-请看一下 - Segue from TableViewCell NIB to ViewController - Pls have a look CollectionView与TableViewCell中的图像 - CollectionView with images inside a TableViewCell TableViewCell 内的 CollectionView - CollectionView inside TableViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM