将UICollectionView单元格数据传递给不同的ViewController时EXC_BAD_INSTRUCTION [英] EXC_BAD_INSTRUCTION When Passing UICollectionView Cell Data to Different ViewController
问题描述
我有一个基于Firebase数据填充的UICollectionView。我已经创建了自定义单元格来填充UICollectionView:
pre $ import $ UI $
import材料
类PollCell:CollectionViewCell {
var key:String? {
获得{
返回self.key
}
}
@IBOutlet弱var imageView:UIImageView!
@IBOutlet weak var pollQuestion:UILabel!
public required init(coder aDecoder:NSCoder){
super.init(coder:aDecoder)
我尝试访问UICollectionView中单击单元格的pollQuestion变量,并将其传递给另一个ViewController:
override func prepare(for segue:UIStoryboardSegue,sender:Any?){
if segue.identifier ==toPoll{
让pollViewController = segue.destination as! PollController
如果让cell = sender as? PollCell {
pollViewController.passLabel.text = cell.pollQuestion.text
}
}
}
$ b $ p
PollController: 更新:我已经修改了代码,现在收到错误 该应用程序在运行时崩溃,我试图解决: 您必须在
$ p $ import UIKit
类PollController: UIViewController {
@IBOutlet弱var passLabel:UILabel!
覆盖func viewDidLoad(){
super.viewDidLoad()
//在加载视图之后执行其他任何设置。
}
覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建的资源。
code
$ b $ p
$ b
passLabel
在准备(for segue
被调用。
PollController
中声明一个(临时)变量,并在 viewDidLoad
class PollController:UIViewController {
@IBOutlet weak var passLabel:UILabel!
var pass =
覆盖func viewDidLoad(){
super.viewDidLoad()
passLabel.text = pass
}
...
在准备(for segue
设置变量而不是文本
标签的属性:
让pollViewController = segue.destination as! PollController
如果让cell = sender as? PollCell {
pollViewController.pass = cell.pollQuestion.text
}
}
注意:不建议从视图(单元格)收集信息。获取索引路径并从模型(数据源数组)中读取信息。
I have a UICollectionView that I am populating based on Firebase data. I have created custom cells that populate the UICollectionView:
import UIKit
import Material
class PollCell: CollectionViewCell {
var key: String? {
get {
return self.key
}
}
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var pollQuestion: UILabel!
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I am attempting to access the pollQuestion variable of the clicked cell in the UICollectionView and pass it to another ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPoll" {
let pollViewController = segue.destination as! PollController
if let cell = sender as? PollCell {
pollViewController.passLabel.text = cell.pollQuestion.text
}
}
}
PollController:
import UIKit
class PollController: UIViewController {
@IBOutlet weak var passLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
UPDATE: I have revised the code and am now receiving the error
The app is crashing at runtime, and I am trying to resolve:
The crash occurs because the outlet passLabel
is not connected yet when prepare(for segue
is called.
You have to declare a (temporary) variable in PollController
and set the label in viewDidLoad
class PollController: UIViewController {
@IBOutlet weak var passLabel: UILabel!
var pass = ""
override func viewDidLoad() {
super.viewDidLoad()
passLabel.text = pass
}
...
In prepare(for segue
set the variable rather than the text
property of the label:
let pollViewController = segue.destination as! PollController
if let cell = sender as? PollCell {
pollViewController.pass = cell.pollQuestion.text
}
}
Note: It's not recommended to gather information from the view (cell). Get the index path and read the information from the model (data source array).
这篇关于将UICollectionView单元格数据传递给不同的ViewController时EXC_BAD_INSTRUCTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!