如何使用swift中的segue将信息传递给新的视图控制器
我在做一个简单的项目时遇到了一个问题。这个应用程序基本上只是显示一组图像,当我点击其中一个图像时,它会转换到一个新的视图控制器,并在标签上显示图像的名称 我是斯威夫特的新手。这是否与我在函数中用于填充集合视图的indexPath有关?我试图查看是否可以获取indexPath,然后将其传递到images数组中,以获取名称并在第二个视图控制器中发送名称,但结果为零。任何帮助都将不胜感激 这是我的CollectionViewController如何使用swift中的segue将信息传递给新的视图控制器,swift,xcode,Swift,Xcode,我在做一个简单的项目时遇到了一个问题。这个应用程序基本上只是显示一组图像,当我点击其中一个图像时,它会转换到一个新的视图控制器,并在标签上显示图像的名称 我是斯威夫特的新手。这是否与我在函数中用于填充集合视图的indexPath有关?我试图查看是否可以获取indexPath,然后将其传递到images数组中,以获取名称并在第二个视图控制器中发送名称,但结果为零。任何帮助都将不胜感激 这是我的CollectionViewController class ImagePickerViewCo
class ImagePickerViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView!
var images = Data()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell {
cell.imageView.image = UIImage(named: "\(images.images[indexPath.item])")
return cell
}
return UICollectionViewCell()
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? FeedPickerViewController{
if segue.identifier == "detailViewController_segue"{
let cell = sender as! CollectionViewCell
let indexPath = self.collectionView.indexPath(for: cell)
let imgName = images.images[indexPath!.item]
dest.imgName = imgName
}
}
}
}
这就是我正在使用的ViewController
class DetailViewController: UIViewController {
var imgName: String!
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Posting Image: \(imgName)"
}
}
这是数据类
class Data {
let images = ["bear", "bird", "bridge",
"cabin"]
}
您应该记住indexPath,它是所选单元格的 您可以使用
collectionView:didSelectItemAtIndexPath:
检索所选单元格的索引
selectedIndexPath=indexPath
然后使用选定的IndexPath将选定对象的数据传递给目标视图控制器
dest.imageName=images.images[selectedIndexPath!.item]
试试这个
class ImagePickerViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return DataImage.images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as? CollectionViewCell {
cell.imageView.image = UIImage(named: DataImage.images[indexPath.item])
return cell
}
return UICollectionViewCell()
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let dest = segue.destination as? FeedPickerViewController{
if segue.identifier == "detailViewController_segue"{
let cell = sender as! CollectionViewCell
let indexPath = self.collectionView.indexPath(for: cell)
let imgName = DataImage.images[indexPath!.item]
dest.imgName = imgName
}
}
}
}
- 使用struct而不是Class
首先,您必须将类数据名称更改为另一个名称,因为数据是数据对象的保留关键字!。您的代码意味着segue是从collection view单元格连接的,而不是从controller连接的。我按照您的建议实现了
collectionView:didSelectItemAtIndexPath:
,但是当我运行调试器时,我注意到这个函数根本没有被调用,我选择的路径变量是nil,它进入segue函数并出现在新的view controller中。是否因为单元格中的imageView
?应用程序正在图像而不是单元格上注册点击?请确保在调用didSelectItemAtIndexPath:
后调用segue(prepare
)
class DetailViewController: UIViewController {
var imgName: String!
@IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Posting Image: " + imgName
}
}
struct DataImage {
static var images = ["bear", "bird", "bridge", "cabin"]
}