前提・実現したいこと
ストーリーボード上で
uiviewcontrollerの一部にpageviewcontrollerをいれ、その中にuicollectionviewcontrollerを配置し、cellには画像をいれています。
実現したいのは、uicollectionviewcontrollerで画像を選択すると、uiviewcontrollerのimageviewに選択された画像が表示されるようにしたいです。
現在は以下までできています。
発生している問題・エラーメッセージ
collectionviewのdidselectメソッドで画像の名前を取得し、値を渡したいのですが、uiimageviewがnilになってしまいます。
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
該当のソースコード
PostViewController
import UIKit class PostViewController: UIViewController, UITextFieldDelegate, UIPageViewControllerDataSource { @IBOutlet weak var bgAnimationView: UIView! @IBOutlet weak var emotionImage: UIImageView! let sboard: UIStoryboard? = UIStoryboard(name:"Main", bundle:nil) var pageViewControllers: [UIViewController] = [] var defaultStampImageNames: [String] = [] var myStampImageNames: [String] = [] var selectedImage: UIImage? { didSet{ self.emotionImage!.image = self.selectedImage self.emotionImage.setNeedsLayout() } } func setup() { // stamp画像の取得 defaultStampImageNames = ["heart_icon", "heart_icon"] myStampImageNames = ["heart_icon", "heart_icon"] } override func viewDidLoad() { super.viewDidLoad() setup() //スタンプ選択フィールドのページング実装 let defaultStampViewController: DefaultStampViewController = sboard!.instantiateViewController(withIdentifier: "DefaultStampViewController") as! DefaultStampViewController //スタンプ画像をコレクションビューに設定 defaultStampViewController.stampImages = defaultStampImageNames let myStampViewController: MyStampViewController = sboard!.instantiateViewController(withIdentifier: "MyStampViewController") as! MyStampViewController //スタンプ画像をコレクションビューに設定 myStampViewController.stampImages = myStampImageNames //UIPageViewControllerの取得 let pageViewController:UIPageViewController? = children[0] as? UIPageViewController //dataSourceに自分自身を設定 pageViewController?.dataSource = self pageViewControllers = [defaultStampViewController,myStampViewController] //UIPageViewControllerに表示対象を設定 pageViewController?.setViewControllers([pageViewControllers[0]], direction: .forward, animated: false, completion: nil) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.view.backgroundColor = UIColor(hex: "FEC82A") startBackgroundAnimation() } //ページコントローラーの処理 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController:UIViewController) -> UIViewController? { //右にスワイプした場合に表示したいviewControllerを返す //ようはページを戻す let index = pageViewControllers.index(of: viewController) if index == 0 { return nil } else { return pageViewControllers[index!-1] } } //ページコントローラーの処理 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { //左にスワイプした場合に表示したいviewControllerを返す //ようはページを進める let index = pageViewControllers.index(of: viewController) if index == pageViewControllers.count-1 { return nil } else { return pageViewControllers[index!+1] } } //リターンを押されたとき func textFieldShouldReturn(_ textField: UITextField) -> Bool { return true } //背景アニメーション func startBackgroundAnimation() { let bgImage = UIImage.init(named: "background_icon")! bgAnimationView.backgroundColor = UIColor.init(patternImage: bgImage) bgAnimationView.frame.size = CGSize(width: view.frame.width + bgImage.size.width, height: view.frame.height + bgImage.size.height) let startOrigin = CGPoint(x: -bgImage.size.width, y: 0) let endOrigin = CGPoint(x: 0, y: -bgImage.size.height) bgAnimationView.frame.origin = startOrigin UIView.animate(withDuration: 1.3, delay: 0.0, options: [.repeat, .curveLinear], animations:{ self.bgAnimationView.frame.origin = endOrigin }, completion: nil) } //閉じる @IBAction func back(_ sender: Any) { dismiss(animated: true, completion: nil) } }
DefaultStampViewController
import UIKit private let reuseIdentifier = "defaultStamp" class DefaultStampViewController: UICollectionViewController { var stampImages: [String] = [] override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Register cell classes // Do any additional setup after loading the view. } // MARK: UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return stampImages.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell :DefaultStampViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! DefaultStampViewCell let cellImg = UIImage(named: stampImages[indexPath.item]) cell.stampImg.image = cellImg cell.backgroundColor = UIColor.clear return cell } override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let selectedImage = UIImage(named: stampImages[indexPath.item])! let postViewController: PostViewController = storyboard!.instantiateViewController(withIdentifier: "Post") as! PostViewController //postViewController.setImage(newImage: selectedImage) postViewController.selectedImage = selectedImage } }
試したこと
調べてみたところ
let _ = self.view
をしてviewの再描画?をするなどの対処法があったのですが、使い方がわからずいれてみてもこれも上手くいかず、、、
(他に入れ方があるのか?)
どなたか助けてください!????
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/21 11:52
2019/02/22 00:41 編集
2019/02/25 04:13
2019/02/25 05:45