FirstViewController(UIViewController)から
CollectionViewCell(UICollectionViewCell)へ値を渡す方法をコードのみでの実現したいと思っています。
一連の動作をする4ファイルのコードを記載しました。
ご確認いただければ幸いです よろしくお願いします。
import UIKit class PresentationController: UICollectionViewController, UICollectionViewDelegateFlowLayout { private var Button: UIBarButtonItem! private let cellId = "cellId" override func viewDidLoad() { super.viewDidLoad() set() } func set() { collectionView?.delegate = self collectionView?.dataSource = self collectionView?.register(CollectionViewCell, forCellWithReuseIdentifier: "cellId") } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 1 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.item { default: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CollectionViewCell } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.item { default: return CGSize(width: view.frame.width, height: 300) } } //ここへ @objc func openButton(_ sender: UIButton) { print("変更するモーダルビュー 開いたよ") //モーダルの動き .custom let modalViewController = FirstViewController() modalViewController.modalPresentationStyle = .custom modalViewController.transitioningDelegate = self present(modalViewController, animated: true, completion: nil) } // override func viewWillAppear(_ animated: Bool) { collectionView?.reloadData() } }
import UIKit class FirstViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white set() } let userName: UITextField = { let un = UITextField() un.backgroundColor = .gray un.text = "" un.frame = CGRect(x: 100, y: 120, width: 200, height: 40) un.returnKeyType = .done return un }() let saveButton: UIButton = { let c = UIButton() c.frame = CGRect(x: 200, y: 0, width: 100, height: 100) c.setTitle("保存", for: .normal) c.backgroundColor = .gray return c }() let cancelbutton: UIButton = { let b = UIButton() b.frame = CGRect(x: 0, y: 0, width: 100, height: 100) b.setTitle("キャンセル", for: .normal) b.backgroundColor = .green return b }() func set() { //初期化時 カラにする userName.text = "" view.addSubview(userName) userName.delegate = self view.addSubview(saveButton) //初期化時は タップ無効化 saveButton.isEnabled = false //ターゲット このボタンをダイレクトにタップした時の判定は⬇️の パターン2にあるよ saveButton.addTarget(self, action: #selector(save2(_:)), for: .touchUpInside) view.addSubview(cancelbutton) cancelbutton.addTarget(self, action: #selector(close(_:)), for: .touchUpInside) } //パターン???? //"return"をタップ キーボード閉じる func textFieldShouldReturn(_ textField: UITextField) -> Bool { //"return"でtextFieldが閉じる(userName, place, siteなど) textField.resignFirstResponder() //TextFieldに入力された値に反応して、その値を習得。 NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeNotifyTextField(sender:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil) return true } //senderの中に 変更があったUITextFieldが入ってくる感じ @objc public func changeNotifyTextField (sender: NSNotification) { guard let textView = sender.object as? UITextField else { return } //文字が1以上で有効に saveButton.isEnabled = (userName.text?.count)! > 0 //タップ処理を登録 saveButton.addTarget(self, action: #selector(save3(_:)), for: .touchUpInside) } //???? //パターン???? //saveButtonをダイレクトにタップ @objc func save2(_ sender: UITapGestureRecognizer) { NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeNotifyTextField2(sender:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil) } //senderの中に 変更があったUITextFieldが入ってくる感じ @objc public func changeNotifyTextField2 (sender: NSNotification) { guard let textView = sender.object as? UITextField else { return } //文字が1以上で有効に saveButton.isEnabled = (userName.text?.count)! > 0 //タップ処理を登録 saveButton.addTarget(self, action: #selector(save3(_:)), for: .touchUpInside) } @objc func save3(_ sender: UITapGestureRecognizer) { //値を渡したい let controller = presentingViewController as! CollectionViewCell controller.textFromModal = userName.text! self.dismiss(animated: true, completion: nil) print("タップ キーボードとモーダルビュー 閉じたよ") saveButton.endEditing(true) } //???? @objc func close(_ sender: UITapGestureRecognizer) { print("保存キャンセル モーダルビュー 閉じたよ") self.dismiss(animated: true, completion: nil) } }
import UIKit class CollectionViewCell: UICollectionViewCell { //モーダルビューから受け取るテキスト var textFromModal = "" { didSet { // 値が更新されたら、ラベルの内容も更新する updateLabel(text: textFromModal) } } //ラベルの内容を更新する処理 func updateLabel(text: String) { nameLabel.text = "" } override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .gray set() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } let button: UIButton = { let b = UIButton() b.setTitle("モーダル", for: .normal) b.backgroundColor = .blue b.translatesAutoresizingMaskIntoConstraints = false b.addTarget(self, action: #selector(PresentationController.openButton(_:)), for: .touchUpInside) b.frame = CGRect(x: 20, y: 50, width: 100, height: 100) return b }() let nameLabel: UILabel = { let l = UILabel() l.backgroundColor = .white l.translatesAutoresizingMaskIntoConstraints = false l.frame = CGRect(x: 20, y: 200, width: 200, height: 50) return l }() func set() { addSubview(button) addSubview(nameLabel) } } //UIPresentationControllerで返す クラス名(CustomPresentationController) extension PresentationController: UIViewControllerTransitioningDelegate { func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? { return CustomPresentationController(presentedViewController: presented, presenting: presenting) } }
・追記
名称をSecondViewControllerからCollectionViewCellへ変更しました。紛らわしく申し訳ありません。
回答2件
あなたの回答
tips
プレビュー