2017-05-12 34 views
0

我想通过数据,使用两个VC之间的委托,但我无法得到为什么它不工作。使用Modal Segue在两个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) 

} 

我使用本modaly赛格瑞用于从第一个VC到第二,并显示形成第二到第一

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

+0

您在哪里分配委托? – KKRocks

回答

2

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

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

注:如果你是赛格瑞是一种Push然后用popViewController还是一种Modal比您需要使用dismiss

相关问题