質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

794閲覧

XIBを使ったTableViewCell上の値を画面遷移時に渡す方法

eotw

総合スコア48

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2021/12/02 04:29

編集2021/12/02 08:09

前提・実現したいこと

SwiftでTodoアプリを作成中
XIBを使ったTableViewCell上の値を画面遷移時に他のViewControllerに渡したい

発生している問題・エラーメッセージ

エラーは特に出ていなく、画面遷移後、渡した値が表示されない

該当のソースコード

TableViewCell

Swift

1import UIKit 2 3protocol MainTableViewCellDelegate { 4 func takingButton() 5} 6 7class MainTableViewCell: UITableViewCell { 8 @IBOutlet weak var timeField: UILabel! 9 @IBOutlet weak var medicineField: UILabel! 10 @IBOutlet weak var mainBackground: UIView! 11 12 var mainTableViewCellDelegate: MainTableViewCellDelegate? 13 var selectMedicine = "" 14 var itemMedicine = "" 15 16 override func awakeFromNib() { 17 super.awakeFromNib() 18 // Initialization code 19 } 20 21 override func setSelected(_ selected: Bool, animated: Bool) { 22 super.setSelected(selected, animated: animated) 23 24 // Configure the view for the selected state 25 } 26 27 func prepare(for segue: UIStoryboardSegue, sender: Any?) { 28 let judgeVC = segue.destination as! JudgementViewController 29 judgeVC.selectedMedicine = itemMedicine 30 31 } 32 33 @IBAction func addAction(_ sender: Any) { 34 mainTableViewCellDelegate?.takingButton() 35 let itemMedicine = medicineField.text 36 } 37}

渡し先のViewController

Swift

1import UIKit 2import RealmSwift 3 4class JudgementViewController: UIViewController { 5 @IBOutlet weak var itemField: UILabel! 6 7 let realm = try! Realm() 8 var selectedMedicine = "" 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 itemField.text = selectedMedicine 14 15 // Do any additional setup after loading the view. 16 } 17 @IBAction func backAction(_ sender: Any) { 18 dismiss(animated: true, completion: nil) 19 }

委託側のViewController

Swift

1import UIKit 2import RealmSwift 3 4class RemindViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet weak var tableView: UITableView! 7 @IBOutlet weak var editButton: UIButton! 8 9 let realm = try! Realm() 10 var itemList:Results<TodoItem>! 11 var token:NotificationToken! 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 tableView.register(UINib(nibName: "MainTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell") 17 editButton.layer.cornerRadius = 10 18 19 itemList = realm.objects(TodoItem.self).sorted(byKeyPath: "date") 20 token = realm.observe{ notification, realm in 21 22 23 self.tableView.reloadData() 24 } 25 } 26 27 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 28 return true 29 } 30 31 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 32 return 150 33 } 34 35 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 36 if editingStyle == .delete { 37 infoHelper().deleteItem(item: itemList[indexPath.row], token: token) 38 tableView.deleteRows(at: [indexPath], with: .automatic) 39 } 40 } 41 42 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 43 return itemList.count 44 } 45 46 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 47 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! MainTableViewCell 48 let item = itemList[indexPath.row] 49 cell.medicineField?.text = item.name 50 cell.timeField?.text = infoHelper().dateToString(date: item.date) 51 cell.mainBackground.layer.cornerRadius = 10 52 cell.mainTableViewCellDelegate = self 53 54 return cell 55 } 56 57 @objc func buttonEvent(_ sender: UIButton) { 58 print("buttonTag", sender.tag) 59 } 60 61} 62 63 64extension RemindViewController: MainTableViewCellDelegate { 65 func takingButton() { 66 performSegue(withIdentifier: "toJudgementVC", sender: nil) 67 } 68}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

MainTableViewCellにprepareメソッドを記述しても画面遷移時に呼び出されることはありません。

prepareメソッドは、preformSegueを実行しているRemindViewControllerの中にoverride指定で記述する必要があります。

渡した値が表示されない原因は、値を渡す処理(prepareメソッド)が動作していないからだと思います。

エラーメッセージが表示されず、何を調べたらいいかわからない時は、何が動作するはずか考えて、それが本当に動作しているかprintを入れたりブレークポイントで止めたりして内部のプログラムの動きをよく調べることをお勧めします。

投稿2021/12/03 05:40

TakeOne

総合スコア6299

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

eotw

2021/12/03 11:52

ありがとうございます! 動作毎に検証しながら進めることにいたします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問