Xcodeでコレクションビューを使ったTODOアプリを作っています。
collectionViewのセルをロングタップすると入力画面に遷移し、次画面にて入力された内容をセル上のラベルにそれぞれ表示したいのですが値が渡らず困っています・・・。
NavigationControllerは使っておらず、segueにはpresentModallyを使っています。エラー等も出ていません。
何か解る方がいましたらご教授お願いしたいです。よろしくお願いします。
swift
1 2import UIKit 3 4struct MyTodoItem { 5 var title: String 6 var dateString: String? 7} 8 9 10class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UIGestureRecognizerDelegate,nextViewControllerDelegate { 11 12 13 14 @IBOutlet weak var todoCollection: UICollectionView! 15 16 let toDos = ["1","2","3","4","5","6"] 17 18 var todos: [MyTodoItem] = [ 19 MyTodoItem(title: "1", dateString: nil), 20 MyTodoItem(title: "2", dateString: nil), 21 MyTodoItem(title: "3", dateString: nil), 22 MyTodoItem(title: "4", dateString: nil), 23 MyTodoItem(title: "5", dateString: nil), 24 MyTodoItem(title: "6", dateString: nil), 25 ] 26 27 28 29 30 override func viewDidLoad() { 31 super.viewDidLoad() 32 33 let layout = UICollectionViewFlowLayout() 34 layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) 35 layout.itemSize = CGSize(width: 100,height: 100) 36 todoCollection.collectionViewLayout = layout 37 38 let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(cellLongPressed)) 39 longPressGestureRecognizer.delegate = self 40 longPressGestureRecognizer.allowableMovement = 15 41 longPressGestureRecognizer.minimumPressDuration = 0.6 42 todoCollection.addGestureRecognizer(longPressGestureRecognizer) 43 44 } 45 46 47 func nextViewController(_ nextVC: NextViewController, didFinishText text: String?) { 48 49 if let editingItem = self.editingItem { 50 51 todos[editingItem].dateString = text 52 53 todoCollection.reloadItems(at: [IndexPath(item: editingItem, section: 0)]) 54 } 55 56 57 } 58 59 60 61 //表示するセルの数 62 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 63 return 6 64 } 65 66 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 67 68 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 69 cell.backgroundColor = .systemPink 70 71 let item = todos[indexPath.row] 72 73 74 let todoLabel = cell.contentView.viewWithTag(1) as! UILabel 75 todoLabel.text = item.title 76 let dateLabel = cell.contentView.viewWithTag(2) as! UILabel 77 dateLabel.text = item.dateString 78 79 return cell 80 81 82 } 83 func collectionview(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 84 let horizontalSpace : CGFloat = 20 85 let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace 86 return CGSize(width: cellSize, height: cellSize) 87 } 88 89 90 override func didReceiveMemoryWarning() { 91 super.didReceiveMemoryWarning() 92 } 93 94 @objc func cellLongPressed(sender: UILongPressGestureRecognizer){ 95 96 if sender.state == UIGestureRecognizer.State.began{ 97 98 performSegue(withIdentifier: "next", sender: nil) 99 100 } 101 102 103 104 } 105 106 107 108 var editingItem: Int? 109 110 111 112 113 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 114 115 116 let nextVC = segue.destination as! NextViewController 117 nextVC.delegate = self 118 119 } 120 121 122 123} 124
swift
1 2import UIKit 3 4 5 6protocol nextViewControllerDelegate { 7 8 func nextViewController(_ nextVC:NextViewController,didFinishText text: String?) 9} 10 11 12class NextViewController: UIViewController,UITextFieldDelegate { 13 14 //デートピッカー 15 var datePicker :UIDatePicker = UIDatePicker() 16 17 18 @IBOutlet weak var dayTextField: UITextField! 19 20 21 var delegate: nextViewControllerDelegate? 22 23 24 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 29 //デートピッカー 30 datePicker.datePickerMode = UIDatePicker.Mode.date 31 datePicker.timeZone = NSTimeZone.local 32 datePicker.locale = Locale(identifier: "ja") 33 dayTextField.inputView = datePicker 34 //デートピッカーのツールバー関係 35 let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 35)) 36 let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: #selector(done)) 37 let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done)); toolBar.setItems([spaceItem,doneItem],animated: true) 38 39 dayTextField.inputView = datePicker 40 dayTextField.inputAccessoryView = toolBar 41 42 } 43 //デートピッカーを閉じるメソッド 44 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 45 46 textField.resignFirstResponder() 47 48 return true 49 } 50 //デートピッカーにdoneボタン 51 52 @ objc func done(){ 53 dayTextField.endEditing(true) 54 55 let formatter = DateFormatter() 56 57 formatter.dateFormat = "yyyy年MM月dd日" 58 59 dayTextField.text = "(formatter.string(from: datePicker.date))" 60 61 } 62 63 override func didReceiveMemoryWarning() { 64 super.didReceiveMemoryWarning() 65 66 } 67 68 69 @IBAction func addButton(_ sender: Any) { 70 71 72 delegate?.nextViewController(self, didFinishText: dayTextField.text) 73 74 self.dismiss(animated: true, completion: nil) 75 76 77 78 79 80 } 81 82 83} 84
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/06 17:14