質問するログイン新規登録

質問編集履歴

1

delegateのセットをしました

2018/04/18 08:39

投稿

kult0922
kult0922

スコア7

title CHANGED
File without changes
body CHANGED
@@ -9,8 +9,7 @@
9
9
  ```
10
10
 
11
11
  ### 該当のソースコード
12
- ```
13
- //データ(text = "aaa")を渡すクラス
12
+ //データを渡すクラス
14
13
  import UIKit
15
14
  protocol AddItemViewControllerDelegate {
16
15
  func addItemViewControllerDidCancel(modalText: String)
@@ -29,74 +28,30 @@
29
28
 
30
29
  override func didReceiveMemoryWarning() {
31
30
  super.didReceiveMemoryWarning()
32
-
33
31
  }
34
32
 
35
- @IBAction func clickedSaveButton(_ sender: UIBarButtonItem) {
33
+ @IBAction func clickSaveButton(_ sender: Any) {
36
- NSLog("clickedSavebutton")
34
+ NSLog("clickedSavebutton")
37
35
  }
38
36
 
39
- @IBAction func clickedCancelButton(_ sender: UIBarButtonItem) {
37
+ @IBAction func clickCancelButton(_ sender: Any) {
40
- //デリケードを実行
41
- self.delegate?.addItemViewControllerDidCancel(modalText: self.text)
38
+ self.delegate?.addItemViewControllerDidCancel(modalText: self.text)
42
- NSLog("clickedCancelbutton")//キャンセルボタンが押されたことを通知
39
+ NSLog("clickedCancelbutton")
43
40
  }
44
41
  }
45
- //データ(text = "aaa")を受けとるクラス
46
- class MasterViewController: UITableViewController, AddItemViewControllerDelegate{
47
42
 
48
- var detailViewController: DetailViewController? = nil
49
- /*中略*/
43
+ //データを受けとるクラス
50
-
51
- func addItemViewControllerDidCancel(modalText: String){
52
- print(modalText)
53
- NSLog("addItemViewControllerDidCancel")//メゾットが実行されたことを通知
54
- }
55
-
56
- }
57
- ```
58
- swift
59
- ```ここに言語名を入力
60
- ソースコード
61
44
  import UIKit
62
- protocol AddItemViewControllerDelegate {
63
- func addItemViewControllerDidCancel(modalText: String)
64
- }
65
-
66
- class AddItemViewController: UITableViewController {
67
- var text:String = "aaa"
68
- //delegate
69
- var delegate: AddItemViewControllerDelegate?
70
-
71
- @IBOutlet weak var textLabel: UITextField!
72
-
73
- override func viewDidLoad() {
74
- super.viewDidLoad()
75
- }
76
-
77
- override func didReceiveMemoryWarning() {
78
- super.didReceiveMemoryWarning()
79
-
80
- }
81
-
82
- @IBAction func clickedSaveButton(_ sender: UIBarButtonItem) {
83
- NSLog("clickedSavebutton")
84
- }
85
-
86
- @IBAction func clickedCancelButton(_ sender: UIBarButtonItem) {
87
- self.delegate?.addItemViewControllerDidCancel(modalText: self.text)
88
- NSLog("clickedCancelbutton")
89
- }
90
- }
91
-
92
45
  class MasterViewController: UITableViewController, AddItemViewControllerDelegate{
93
46
 
94
47
  var detailViewController: DetailViewController? = nil
95
48
  var objects = [Any]()
96
49
 
50
+ let vc = AddItemViewController()
97
51
 
98
52
  override func viewDidLoad() {
99
53
  super.viewDidLoad()
54
+ self.vc.delegate = self//デリゲートをセット
100
55
  // Do any additional setup after loading the view, typically from a nib.
101
56
  navigationItem.leftBarButtonItem = editButtonItem
102
57
 
@@ -106,6 +61,7 @@
106
61
  let controllers = split.viewControllers
107
62
  detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
108
63
  }
64
+
109
65
  }
110
66
 
111
67
  override func viewWillAppear(_ animated: Bool) {
@@ -173,11 +129,10 @@
173
129
 
174
130
  func addItemViewControllerDidCancel(modalText: String){
175
131
  print(modalText)
176
- NSLog("addItemViewControllerDidCancel")
132
+ NSLog("addItemViewControllerDidCancel")//実行されたか確認
133
+ self.vc.dismiss(animated: true, completion: nil)
177
134
  }
178
-
179
135
  }
180
-
181
136
  ```
182
137
 
183
138
  ### 試したこと