简体   繁体   中英

UIImageView passing (segue) nil to another ViewController

after a day of researching and trying stuff, I come to your aid.

I have a collectionView passing an image to an imageView, just like instagram (for you to imagine the interface), I THINK I'm performing the segue right, but on the other viewController it ends up NIL.

My code is as follows:

First 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!
        }
    }

}

Second 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)
    }

}

Can you guys help me?

Based on the fact that the segue.identifier in your prepare(for:) returns nil, that means the segue performed is triggered by the storyboards, not by the postPhotoTaken(_ sender: Any) .

Check the storyboard, and find the segue that goes from first VC to second VC and is triggered by the button, and change it's identifier to "photoPost" .

I believe after that you can delete postPhotoTaken(_ sender: Any) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM