在swift中,当uicollectionview被录制时,将一个图像传递给一个细节视图控制器



我已经做了一段时间了,但似乎无法在swift中破解它

我希望用户能够在uicollectionView中选择图像,并为该图像出现在详细视图控制器中,我可以很容易地用一段文字,我可以在有一个预加载的静态图像数组时这样做。但是我似乎不能得到任何地方与collectionview加载的图像从相机。

我明白我需要使用

override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) {
  }

和此函数分离选定的细胞。

  func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
  }

我确实有这些出口@IBOutlet弱var collectionView: UICollectionView!

  var images = [UIImage]()

图像选择器通过

将所有图像存储到此数组
  images.insert(newImage, atIndex: 0)

当数组传递给detailviewcontroller时,我明白必须将其复制到另一个本地数组中,然后我如何获得首先显示的突出显示的当前图像,可能使用indexPath。行

我没有使用segue,实际上我不太明白你的问题是什么,但我会尽量向你展示如何实现它。

首先,你有一个数组的图像,所以我认为你的图像应该访问图像[indexPath.row]

让我们假设你已经有一个UIViewController类里面有UIImageView。如果是这样,您可以这样写:

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    let myVC = MyViewController()
    myVC.imageView.image = images[indexPath.row]
    self.presentViewController(myVC, animated: true, completion: nil)
}

为modal或

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    let myVC = MyViewController()
    myVC.imageView.image = images[indexPath.row]
    self.navigationController?.pushViewController(myVC, animated: true)
}

如果你想把它显示为导航。

我相信segue基本上是一样的但我认为你必须使用func prepareForSegue来准备segue (func performSegueWithIdentifier会执行它)在prepareForSegue中你应该检查segue的标识符

if segue.identifier == "myIdentifier" {
  //your code here
}

,如果这个标识符是正确的,把你的命令到你的myVC那里。

最新更新