2017-05-12 1 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) 

} 

I 번째 내지 제 VC로부터 위해 본 modaly SEGUE을 사용하고, 쇼 제

내지 제를 형성 두 번째 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) 
} 

주 문제는 대신 당신이 당신의 버튼 동작으로 완전히 새로운 컨트롤러를로드하는 컨트롤러를 보여주고입니다 : 당신이 SEGUE 경우 것은 Push의 종류 다음 popViewController를 사용하거나 그것보다 종류 Modal의입니다 dismiss을 사용해야합니다.

관련 문제