簡體   English   中英

UIImageView傳遞(segue)nil到另一個ViewController

[英]UIImageView passing (segue) nil to another ViewController

經過一天的研究和嘗試,我幫助了您。

我有一個collectionView將圖像傳遞到imageView,就像instagram(讓您想象接口)一樣,我認為我正在正確執行segue,但是在另一個viewController上,它最終為NIL。

我的代碼如下:

第一視圖控制器>

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

}

第二視圖控制器>

//  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是由情節postPhotoTaken(_ sender: Any)觸發的,而不是由postPhotoTaken(_ sender: Any)觸發的。

檢查情節提要,找到從第一個VC到第二個VC並由按鈕觸發的segue,然后將其identifier更改為"photoPost"

我相信之后,您可以刪除postPhotoTaken(_ sender: Any)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM