ios - UIImageView 将(segue)nil 传递给另一个 ViewController

标签 ios swift uiimageview uicollectionview segue

经过一天的研究和尝试,我来帮助你。

我有一个 collectionView 将图像传递给 imageView,就像 instagram(让你想象界面),我认为我正在正确执行 segue,但在另一个 viewController 上它最终为 NIL。

我的代码如下:

第一个 View Controller >

    //  TakePhotoViewController.swift

    import UIKit
    import Photos

    class TakePhotoViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

        @IBOutlet weak var photoImageView: UIImageView!


    var imageArray = [UIImage]()

    override func viewDidLoad(){
        super.viewDidLoad()
        grabPhotos()
    }

    @IBAction func postPhotoTaken(_ sender: Any) {
        self.performSegue(withIdentifier: "photoPost", sender: self)
    }


    func grabPhotos(){

        let imgManager = PHImageManager.default()
        let requestOptions = PHImageRequestOptions()

        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

        if let fetchResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions){
            if fetchResult.count > 0 {
                for i in 0..<fetchResult.count{
                    imgManager.requestImage(for: fetchResult.object(at: i), targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {image, error in

                        self.imageArray.append(image!)

                    })
                }
            }

            else {
                print("You Don't Have Any Photos!")
            }
        }

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        let imageView = cell.viewWithTag(1) as! UIImageView

        imageView.image = imageArray[indexPath.row]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        photoImageView.image = imageArray[indexPath.row]
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let width = collectionView.frame.width / 3 - 1

        return CGSize(width: width, height: width)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "photoPost" {

            let photoPost = segue.destination as! PhotoPostTableViewController

            let imagePhoto = self.photoImageView.image
            photoPost.photo = imagePhoto!
        }
    }

}

第二个 View Controller >

//  photoPostViewController.swift

import UIKit

class PhotoPostTableViewController: UITableViewController {

    var photo: UIImage!

    @IBOutlet weak var newPhoto: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        newPhoto.image = photo
        print(photo)

    }

    override func viewDidAppear(_ animated: Bool) {
        newPhoto.image = photo
        print(photo)
    }

}

你们能帮帮我吗?

最佳答案

基于 prepare(for:) 中的 segue.identifier 返回 nil 的事实,这意味着执行的 segue 是由 Storyboard触发的,而不是postPhotoTaken(_ sender: Any).

检查 Storyboard,找到从第一个 VC 到第二个 VC 并由按钮触发的转场,并将其 identifier 更改为 "photoPost"

我相信之后你可以删除 postPhotoTaken(_ sender: Any)

关于ios - UIImageView 将(segue)nil 传递给另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48410727/

相关文章:

ios - 使用叠加图像 OpenGLES 保存屏幕截图

ios - 将 onClick 事件添加到 MKAnnotation swift

ios - 将方法 isEqualToString 替换为类似 containsString : for a UITextField response 的方法

ios - 将Localizable.strings转换为json

ios - Xcode 快疯了!在编码时,丢失类、引用并且不会自动完成,经常给出 <<error type>>

swift - 在 Xcode 7.3.1 中,为什么所有警告在我的 .swift 文件中都不可见

ios - 如何解锁swift文件(Xcode)?

ios - UIImage 滑动指示器大小

iphone - 警告 : TSD slot 10 retrieved but the thread data has already been torn down

swift - 在媒体繁重的 swift 应用程序中管理内存的正确方法是什么?释放它?