如何准备 segue can't selected pass label text to text string
how to prepare segue can't selected pass label text to text string
我已经准备了一个 segue,但是当用户选择一个单元格时,它没有将标签文本传递给详细查看的文本
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewNumberSegue" {
guard let indexPath = sender as? IndexPath else { return }
let collectionCell = collectionView.cellForItem(at: indexPath) as! ItemCollectionViewCell
let textToPass = collectionCell.ItemLabel.text
let detailVC = segue.destination as? DetailViewController
detailVC?.number = textToPass!
}
}
// MARK: UICollectionViewDelegate
extension GridCollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "viewNumberSegue", sender: indexPath)
}
}
详细VC
import UIKit
class DetailViewController: UIViewController {
var number:String = ""
@IBOutlet weak var numberLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
print(number)
numberLabel.text = number
}
}
与其在sender中传递indexPath,不如直接将number作为sender传递。
extension GridCollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let collectionCell = collectionView.cellForItem(at: indexPath) as! ItemCollectionViewCell
let textToPass = collectionCell.ItemLabel.text
self.performSegue(withIdentifier: "viewNumberSegue", sender: textToPass)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewNumberSegue" {
guard let number = sender as? String else { return }
let detailVC = segue.destination as? DetailViewController
detailVC?.number = number
}
}
我已经准备了一个 segue,但是当用户选择一个单元格时,它没有将标签文本传递给详细查看的文本
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewNumberSegue" {
guard let indexPath = sender as? IndexPath else { return }
let collectionCell = collectionView.cellForItem(at: indexPath) as! ItemCollectionViewCell
let textToPass = collectionCell.ItemLabel.text
let detailVC = segue.destination as? DetailViewController
detailVC?.number = textToPass!
}
}
// MARK: UICollectionViewDelegate
extension GridCollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "viewNumberSegue", sender: indexPath)
}
}
详细VC
import UIKit
class DetailViewController: UIViewController {
var number:String = ""
@IBOutlet weak var numberLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
print(number)
numberLabel.text = number
}
}
与其在sender中传递indexPath,不如直接将number作为sender传递。
extension GridCollectionViewController {
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let collectionCell = collectionView.cellForItem(at: indexPath) as! ItemCollectionViewCell
let textToPass = collectionCell.ItemLabel.text
self.performSegue(withIdentifier: "viewNumberSegue", sender: textToPass)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewNumberSegue" {
guard let number = sender as? String else { return }
let detailVC = segue.destination as? DetailViewController
detailVC?.number = number
}
}