【问题标题】:UIImageView passing (segue) nil to another ViewControllerUIImageView 将 (segue) nil 传递给另一个 ViewController
【发布时间】:2018-01-23 21:01:23
【问题描述】:

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

我有一个 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)
    }

}

你们能帮帮我吗?

【问题讨论】:

  • 如果让 imagePhoto = self.photoImageView.image 中断,self.photoImageView.image 的值是多少?那条线会被调用吗?
  • 对不起,我是 Swift 编程的新手,我添加了断点,但似乎根本没有调用它。我什至在该行下方写了一个打印并添加了一个断点,但控制台上什么也没出现。
  • if segue.identifier == "photoPost" { 之前添加以下行:print("&gt;&gt;&gt; \(segue.identifier)") 以找出执行的segue 的标识符是什么 - 然后您可以将条件更改为该新代码
  • 你的segue标识符真的是“photoPost”吗?
  • postPhotoTaken() 他正在尝试执行那个segue,所以我猜他有一些错误的连接

标签: ios swift uiimageview uicollectionview segue


【解决方案1】:

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

查看情节提要,找到从第一个 VC 到第二个 VC 并由按钮触发的 segue,并将其更改为 identifier"photoPost"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多