【问题标题】:Passing image between two VC using Modal Segue使用 Modal Segue 在两个 VC 之间传递图像
【发布时间】:2017-10-11 07:51:40
【问题描述】:

我正在尝试在两个 VC 之间使用委托传递数据,但我不明白为什么它不起作用。

我的第一个 VC

class ViewController: UIViewController {

   @IBOutlet weak var profileImage: UIImageView!

    func downloadPhoto() {

        self.performSegue(withIdentifier: "showPhotoFromInternet", sender: nil)

    }

}

extension ViewController: IPresentaionPhotoFormInternetDelegate {

    func setNewImage(imageToShow: UIImage) {
        profileImage.image = imageToShow
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? PresentPhotoFromInternetViewController {
            destination.delegate = self
        }
    }

我的第二个 VC

class PresentPhotoFromInternetViewController: UIViewController { 

 var imageToSend: UIImage?
 var delegate: IPresentaionPhotoFormInternetDelegate?
@IBOutlet weak var photoCollectionView: UICollectionView!

 override func viewDidLoad() {
        super.viewDidLoad()

        photoCollectionView.allowsMultipleSelection = false

}

    @IBAction func sendPhotoToPreviousController(_ sender: Any) {

        delegate?.setNewImage(imageToShow: iamgeToSend!)
        performSegue(withIdentifier: "sendPhoto", sender: nil)

    }

extension PresentPhotoFromInternetViewController: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cell = collectionView.cellForItem(at: indexPath) as! photoCollectionViewCell

        print("Cell is selected")
        iamgeToSend = cell.photoImageView.image
        cell.selectedView.backgroundColor = UIColor.green
    }

protocol IPresentaionPhotoFormInternetDelegate {

    func setNewImage(imageToShow: UIImage)

}

我使用现在情态从第一个 VC 到第二个,并显示从第二个到第一个

当我从第二个 VC 执行 segue 时,我的第一个 VC 没有更新,尽管它通过了所有断点。

【问题讨论】:

  • 你在哪里分配委托?

标签: ios uiviewcontroller delegates


【解决方案1】:

问题不是弹出控制器,而是在按钮操作中加载全新的控制器

@IBAction func sendPhotoToPreviousController(_ sender: Any) {

    delegate?.setNewImage(imageToShow: iamgeToSend!)
    //Comment or remove the performSegue 
    //performSegue(withIdentifier: "sendPhoto", sender: nil)

    //Now simply pop this controller
    _ = self.navigationController?.popViewController(animated: true)

    // If you are presenting this controller then you need to dismiss
    self.dismiss(animated: true)
}

注意:如果你的 segue 是 Push,那么使用 popViewController 或者是 Modal,而不是你需要使用 dismiss

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多