我有一个基于Firebase数据填充的UICollectionView。我创建了填充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)
}
}
我正在尝试访问UICollectionView中单击的单元格的pollQuestion变量,并将其传递给另一个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.
}
}
更新:我修改了代码,现在收到错误
应用程序在运行时崩溃,我正在尝试解决:
答案 0 :(得分:2)
发生崩溃是因为在调用passLabel
时,插座prepare(for segue
尚未连接。
您必须在PollController
中声明一个(临时)变量,并在viewDidLoad
class PollController: UIViewController {
@IBOutlet weak var passLabel: UILabel!
var pass = ""
override func viewDidLoad() {
super.viewDidLoad()
passLabel.text = pass
}
...
在prepare(for segue
中设置变量而不是标签的text
属性:
let pollViewController = segue.destination as! PollController
if let cell = sender as? PollCell {
pollViewController.pass = cell.pollQuestion.text
}
}
注意:建议不要从视图(单元格)中收集信息。获取索引路径并从模型(数据源数组)中读取信息。
答案 1 :(得分:1)
您应该使用传递给您作为发件人的单元格,而不是调用为您提供新单元格的cellForItem。
class client{
use sampletrait {
hello as protected sampletrait_hello;
}
function hello(){
$this->sampletrait_hello();
echo "hello from class";
}
}