

Xcodeでコレクションビューを使ったTODOアプリを作っています。  
collectionViewのセルをロングタップすると入力画面に遷移し、次画面にて入力された内容をセル上のラベルにそれぞれ表示したいのですが値が渡らず困っています・・・。  
NavigationControllerは使っておらず、segueにはpresentModallyを使っています。エラー等も出ていません。  
何か解る方がいましたらご教授お願いしたいです。よろしくお願いします。

[code]

    import UIKit
    
    struct MyTodoItem {
        var title: String
        var dateString: String? 
    }
    
    
    class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate,nextViewControllerDelegate {
    
    
    
        @IBOutlet weak var todoCollection: UICollectionView!
    
        let toDos = ["１","２","３","４","５","６"]
    
        var todos: [MyTodoItem] = [
            MyTodoItem(title: "１", dateString: nil),
            MyTodoItem(title: "２", dateString: nil),
            MyTodoItem(title: "３", dateString: nil),
            MyTodoItem(title: "４", dateString: nil),
            MyTodoItem(title: "５", dateString: nil),
            MyTodoItem(title: "６", dateString: nil),
        ]
    
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
            layout.itemSize = CGSize(width: 100,height: 100)
            todoCollection.collectionViewLayout = layout
    
            let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(cellLongPressed))
            longPressGestureRecognizer.delegate = self
            longPressGestureRecognizer.allowableMovement = 15
            longPressGestureRecognizer.minimumPressDuration = 0.6
            todoCollection.addGestureRecognizer(longPressGestureRecognizer)
    
        }
    
    
        func nextViewController(_ nextVC: NextViewController, didFinishText text: String?) {
    
           if let editingItem = self.editingItem {
    
                     todos[editingItem].dateString = text
    
                       todoCollection.reloadItems(at: [IndexPath(item: editingItem, section: 0)])
                   }
    
    
        }
    
    
    
        //表示するセルの数
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 6
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
            cell.backgroundColor = .systemPink
    
            let item = todos[indexPath.row]
    
    
            let todoLabel = cell.contentView.viewWithTag(1) as! UILabel
            todoLabel.text = item.title 
            let dateLabel = cell.contentView.viewWithTag(2) as! UILabel
            dateLabel.text = item.dateString       
    
            return cell
    
    
        }
        func collectionview(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let horizontalSpace : CGFloat = 20
            let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace
            return CGSize(width: cellSize, height: cellSize)
        }
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        @objc func cellLongPressed(sender: UILongPressGestureRecognizer){
    
            if sender.state == UIGestureRecognizer.State.began{
    
               performSegue(withIdentifier: "next", sender: nil)
    
            }
    
    
    
        }
    
    
    
        var  editingItem: Int?
    
    
    
    
       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    
                  let nextVC = segue.destination as! NextViewController
                   nextVC.delegate = self
    
               }
    
    
    
    }
    
    
    
[/code]

[code]

    import UIKit
    
    
    
    protocol nextViewControllerDelegate {
    
        func nextViewController(_ nextVC:NextViewController,didFinishText text: String?)
    }
    
    
    class NextViewController: UIViewController,UITextFieldDelegate {
    
        //デートピッカー
        var datePicker :UIDatePicker = UIDatePicker()
    
    
        @IBOutlet weak var dayTextField: UITextField!
    
    
       var delegate: nextViewControllerDelegate?
    
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        //デートピッカー
            datePicker.datePickerMode = UIDatePicker.Mode.date
            datePicker.timeZone = NSTimeZone.local
            datePicker.locale = Locale(identifier: "ja")
            dayTextField.inputView = datePicker
        //デートピッカーのツールバー関係
            let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35))
            let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: #selector(done))
            let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done)); toolBar.setItems([spaceItem,doneItem],animated: true)
    
            dayTextField.inputView = datePicker
            dayTextField.inputAccessoryView = toolBar
    
        }
       //デートピッカーを閉じるメソッド
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
            textField.resignFirstResponder()
    
            return true
        }
      //デートピッカーにdoneボタン
    
        @ objc func done(){
            dayTextField.endEditing(true)
    
            let formatter = DateFormatter()
    
            formatter.dateFormat = "yyyy年MM月dd日"
    
            dayTextField.text = "\(formatter.string(from: datePicker.date))"
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
    
        }
    
    
        @IBAction func addButton(_ sender: Any) {
    
    
            delegate?.nextViewController(self, didFinishText: dayTextField.text)
    
                   self.dismiss(animated: true, completion: nil)
    
    
    
    
    
        }
    
    
    }
    
[/code]

[code]

    swift
    
     import UIKit
    
    
    
    struct MyTodoItem {
        var title: String
        var dateString: String? 
    
    }
    
    
        class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate,nextViewControllerDelegate {
    
    
    
    
    
        @IBOutlet weak var babyImage: UIImageView!
    
    
        @IBOutlet weak var todoCollection: UICollectionView!
    
        let toDos = ["１","２","３","４","５","６"]
    
    
        var todos: [MyTodoItem] = [
            MyTodoItem(title: "１", dateString: nil),
            MyTodoItem(title: "２", dateString: nil),
            MyTodoItem(title: "３", dateString: nil),
            MyTodoItem(title: "４", dateString: nil),
            MyTodoItem(title: "５", dateString: nil),
            MyTodoItem(title: "６", dateString: nil),
        ]
    
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
            layout.itemSize = CGSize(width: 100,height: 100)
            todoCollection.collectionViewLayout = layout
    
            let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(cellLongPressed))
            longPressGestureRecognizer.delegate = self
            longPressGestureRecognizer.allowableMovement = 15
            longPressGestureRecognizer.minimumPressDuration = 0.6
            todoCollection.addGestureRecognizer(longPressGestureRecognizer)
    
        }
    
        func nextViewController(_ nextVC: NextViewController, didFinishText text: String?) {
    
              if let editingItem = self.editingItem {
    
                        todos[editingItem].dateString = text
    
                          todoCollection.reloadItems(at: [IndexPath(item: editingItem, section: 0)])
                      }
    
    
    
            }
    
    
        //表示するセルの数
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 6
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
            //セルの色
            cell.backgroundColor = .systemPink
    
            let item = todos[indexPath.row]
    
    
            let todoLabel = cell.contentView.viewWithTag(1) as! UILabel
            todoLabel.text = item.title //<-
            let dateLabel = cell.contentView.viewWithTag(2) as! UILabel
            dateLabel.text = item.dateString//<-
    
    
    
            return cell
    
    
        }
        func collectionview(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let horizontalSpace : CGFloat = 20
            let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace
            return CGSize(width: cellSize, height: cellSize)
        }
    
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        @objc func cellLongPressed(sender: UILongPressGestureRecognizer){
    
    
            if sender.state == UIGestureRecognizer.State.began{
    
                performSegue(withIdentifier: "next", sender: nil)
    
    
    
    
            }
    
    
    
    
        }
    
    
    
        var  editingItem: Int?
    
    
    
    
       override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier == "next" {
    
                   let cell = sender as! UICollectionViewCell
    
                   if let indexPath = todoCollection.indexPath(for: cell) {
                       editingItem = indexPath.item
                   } else {
                       editingItem = nil
                   }
    
    
           let nextVC = segue.destination as! NextViewController
                        nextVC.delegate = self
    
               }
    
    
    
    }
    
    
    
    }
    
    
    
    
[/code]


CC BY-SA 4.0
2019-10-02T15:47:40.293
59422
2019-10-06T16:54:53.580
2019-10-06T16:54:53.580
34757
34757
question
swift ios xcode
swift 値渡しがされない場合の解決方法