前提・実現したいこと
cancelボタンを押した時にAddItemViewControllerクラスのテキストをMasterViewControllerクラスにデリケードを使用して渡したい
発生している問題・エラーメッセージ
デリゲートで呼ばれるメゾットの内容が実行されない
エラーメッセージ エラーは出ていません
該当のソースコード
//データを渡すクラス
import UIKit
protocol AddItemViewControllerDelegate {
func addItemViewControllerDidCancel(modalText: String)
}
class AddItemViewController: UITableViewController {
var text:String = "aaa"
//delegate
var delegate: AddItemViewControllerDelegate?
@IBOutlet weak var textLabel: UITextField! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func clickSaveButton(_ sender: Any) { NSLog("clickedSavebutton") } @IBAction func clickCancelButton(_ sender: Any) { self.delegate?.addItemViewControllerDidCancel(modalText: self.text) NSLog("clickedCancelbutton") }
}
//データを受けとるクラス
import UIKit
class MasterViewController: UITableViewController, AddItemViewControllerDelegate{
var detailViewController: DetailViewController? = nil var objects = [Any]() let vc = AddItemViewController() override func viewDidLoad() { super.viewDidLoad() self.vc.delegate = self//デリゲートをセット // Do any additional setup after loading the view, typically from a nib. navigationItem.leftBarButtonItem = editButtonItem //let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:))) //navigationItem.rightBarButtonItem = addButton if let split = splitViewController { let controllers = split.viewControllers detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController } } override func viewWillAppear(_ animated: Bool) { clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed super.viewWillAppear(animated) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @objc func insertNewObject(_ sender: Any) { objects.insert(NSDate(), at: 0) let indexPath = IndexPath(row: 0, section: 0) tableView.insertRows(at: [indexPath], with: .automatic) } // MARK: - Segues override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "showDetail" { if let indexPath = tableView.indexPathForSelectedRow { let object = objects[indexPath.row] as! NSDate let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController controller.detailItem = object controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem controller.navigationItem.leftItemsSupplementBackButton = true } } } // MARK: - Table View override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return objects.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let object = objects[indexPath.row] as! NSDate cell.textLabel!.text = object.description return cell } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // Return false if you do not want the specified item to be editable. return true } override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { objects.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .fade) } else if editingStyle == .insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } func addItemViewControllerDidCancel(modalText: String){ print(modalText) NSLog("addItemViewControllerDidCancel")//実行されたか確認 self.vc.dismiss(animated: true, completion: nil) }
}
### 試したこと 挙動を確認するためにNSLog("addItemViewControllerDidCancel")をaddItemViewControllerDidCancelに入れたが何も表示されなかったのでaddItemViewControllerDidCancel自体が呼び出されていない可能性があると思いますが原因が分からず困っています。キャンセルボタンを押すとclickedCancelbuttonのみ表示されてaddItemViewControllerDidCancelが表示されない状況となっています。 ### 補足情報(FW/ツールのバージョンなど) Swift version 4.1 ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー