質問編集履歴

1

delegateのセットをしました

2018/04/18 08:39

投稿

kult0922
kult0922

スコア7

test CHANGED
File without changes
test CHANGED
@@ -20,346 +20,256 @@
20
20
 
21
21
  ### 該当のソースコード
22
22
 
23
+ //データを渡すクラス
24
+
25
+ import UIKit
26
+
27
+ protocol AddItemViewControllerDelegate {
28
+
29
+ func addItemViewControllerDidCancel(modalText: String)
30
+
31
+ }
32
+
33
+
34
+
35
+ class AddItemViewController: UITableViewController {
36
+
37
+ var text:String = "aaa"
38
+
39
+ //delegate
40
+
41
+ var delegate: AddItemViewControllerDelegate?
42
+
43
+
44
+
45
+ @IBOutlet weak var textLabel: UITextField!
46
+
47
+
48
+
49
+ override func viewDidLoad() {
50
+
51
+ super.viewDidLoad()
52
+
53
+ }
54
+
55
+
56
+
57
+ override func didReceiveMemoryWarning() {
58
+
59
+ super.didReceiveMemoryWarning()
60
+
61
+ }
62
+
63
+
64
+
65
+ @IBAction func clickSaveButton(_ sender: Any) {
66
+
67
+ NSLog("clickedSavebutton")
68
+
69
+ }
70
+
71
+
72
+
73
+ @IBAction func clickCancelButton(_ sender: Any) {
74
+
75
+ self.delegate?.addItemViewControllerDidCancel(modalText: self.text)
76
+
77
+ NSLog("clickedCancelbutton")
78
+
79
+ }
80
+
81
+ }
82
+
83
+
84
+
85
+ //データを受けとるクラス
86
+
87
+ import UIKit
88
+
89
+ class MasterViewController: UITableViewController, AddItemViewControllerDelegate{
90
+
91
+
92
+
93
+ var detailViewController: DetailViewController? = nil
94
+
95
+ var objects = [Any]()
96
+
97
+
98
+
99
+ let vc = AddItemViewController()
100
+
101
+
102
+
103
+ override func viewDidLoad() {
104
+
105
+ super.viewDidLoad()
106
+
107
+ self.vc.delegate = self//デリゲートをセット
108
+
109
+ // Do any additional setup after loading the view, typically from a nib.
110
+
111
+ navigationItem.leftBarButtonItem = editButtonItem
112
+
113
+
114
+
115
+ //let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
116
+
117
+ //navigationItem.rightBarButtonItem = addButton
118
+
119
+ if let split = splitViewController {
120
+
121
+ let controllers = split.viewControllers
122
+
123
+ detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
124
+
125
+ }
126
+
127
+
128
+
129
+ }
130
+
131
+
132
+
133
+ override func viewWillAppear(_ animated: Bool) {
134
+
135
+ clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
136
+
137
+ super.viewWillAppear(animated)
138
+
139
+ }
140
+
141
+
142
+
143
+ override func didReceiveMemoryWarning() {
144
+
145
+ super.didReceiveMemoryWarning()
146
+
147
+ // Dispose of any resources that can be recreated.
148
+
149
+ }
150
+
151
+
152
+
153
+ @objc
154
+
155
+ func insertNewObject(_ sender: Any) {
156
+
157
+ objects.insert(NSDate(), at: 0)
158
+
159
+ let indexPath = IndexPath(row: 0, section: 0)
160
+
161
+ tableView.insertRows(at: [indexPath], with: .automatic)
162
+
163
+ }
164
+
165
+
166
+
167
+ // MARK: - Segues
168
+
169
+
170
+
171
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
172
+
173
+ if segue.identifier == "showDetail" {
174
+
175
+ if let indexPath = tableView.indexPathForSelectedRow {
176
+
177
+ let object = objects[indexPath.row] as! NSDate
178
+
179
+ let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
180
+
181
+ controller.detailItem = object
182
+
183
+ controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
184
+
185
+ controller.navigationItem.leftItemsSupplementBackButton = true
186
+
187
+ }
188
+
189
+ }
190
+
191
+ }
192
+
193
+
194
+
195
+ // MARK: - Table View
196
+
197
+
198
+
199
+ override func numberOfSections(in tableView: UITableView) -> Int {
200
+
201
+ return 1
202
+
203
+ }
204
+
205
+
206
+
207
+ override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
208
+
209
+ return objects.count
210
+
211
+ }
212
+
213
+
214
+
215
+ override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
216
+
217
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
218
+
219
+
220
+
221
+ let object = objects[indexPath.row] as! NSDate
222
+
223
+ cell.textLabel!.text = object.description
224
+
225
+ return cell
226
+
227
+ }
228
+
229
+
230
+
231
+ override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
232
+
233
+ // Return false if you do not want the specified item to be editable.
234
+
235
+ return true
236
+
237
+ }
238
+
239
+
240
+
241
+ override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
242
+
243
+ if editingStyle == .delete {
244
+
245
+ objects.remove(at: indexPath.row)
246
+
247
+ tableView.deleteRows(at: [indexPath], with: .fade)
248
+
249
+ } else if editingStyle == .insert {
250
+
251
+ // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ func addItemViewControllerDidCancel(modalText: String){
260
+
261
+ print(modalText)
262
+
263
+ NSLog("addItemViewControllerDidCancel")//実行されたか確認
264
+
265
+ self.vc.dismiss(animated: true, completion: nil)
266
+
267
+ }
268
+
269
+ }
270
+
23
271
  ```
24
272
 
25
- //データ(text = "aaa")を渡すクラス
26
-
27
- import UIKit
28
-
29
- protocol AddItemViewControllerDelegate {
30
-
31
- func addItemViewControllerDidCancel(modalText: String)
32
-
33
- }
34
-
35
-
36
-
37
- class AddItemViewController: UITableViewController {
38
-
39
- var text:String = "aaa"
40
-
41
- //delegate
42
-
43
- var delegate: AddItemViewControllerDelegate?
44
-
45
-
46
-
47
- @IBOutlet weak var textLabel: UITextField!
48
-
49
-
50
-
51
- override func viewDidLoad() {
52
-
53
- super.viewDidLoad()
54
-
55
- }
56
-
57
-
58
-
59
- override func didReceiveMemoryWarning() {
60
-
61
- super.didReceiveMemoryWarning()
62
-
63
-
64
-
65
- }
66
-
67
-
68
-
69
- @IBAction func clickedSaveButton(_ sender: UIBarButtonItem) {
70
-
71
- NSLog("clickedSavebutton")
72
-
73
- }
74
-
75
-
76
-
77
- @IBAction func clickedCancelButton(_ sender: UIBarButtonItem) {
78
-
79
- //デリケードを実行
80
-
81
- self.delegate?.addItemViewControllerDidCancel(modalText: self.text)
82
-
83
- NSLog("clickedCancelbutton")//キャンセルボタンが押されたことを通知
84
-
85
- }
86
-
87
- }
88
-
89
- //データ(text = "aaa")を受けとるクラス
90
-
91
- class MasterViewController: UITableViewController, AddItemViewControllerDelegate{
92
-
93
-
94
-
95
- var detailViewController: DetailViewController? = nil
96
-
97
- /*中略*/
98
-
99
-
100
-
101
- func addItemViewControllerDidCancel(modalText: String){
102
-
103
- print(modalText)
104
-
105
- NSLog("addItemViewControllerDidCancel")//メゾットが実行されたことを通知
106
-
107
- }
108
-
109
-
110
-
111
- }
112
-
113
- ```
114
-
115
- swift
116
-
117
- ```ここに言語名を入力
118
-
119
- ソースコード
120
-
121
- import UIKit
122
-
123
- protocol AddItemViewControllerDelegate {
124
-
125
- func addItemViewControllerDidCancel(modalText: String)
126
-
127
- }
128
-
129
-
130
-
131
- class AddItemViewController: UITableViewController {
132
-
133
- var text:String = "aaa"
134
-
135
- //delegate
136
-
137
- var delegate: AddItemViewControllerDelegate?
138
-
139
-
140
-
141
- @IBOutlet weak var textLabel: UITextField!
142
-
143
-
144
-
145
- override func viewDidLoad() {
146
-
147
- super.viewDidLoad()
148
-
149
- }
150
-
151
-
152
-
153
- override func didReceiveMemoryWarning() {
154
-
155
- super.didReceiveMemoryWarning()
156
-
157
-
158
-
159
- }
160
-
161
-
162
-
163
- @IBAction func clickedSaveButton(_ sender: UIBarButtonItem) {
164
-
165
- NSLog("clickedSavebutton")
166
-
167
- }
168
-
169
-
170
-
171
- @IBAction func clickedCancelButton(_ sender: UIBarButtonItem) {
172
-
173
- self.delegate?.addItemViewControllerDidCancel(modalText: self.text)
174
-
175
- NSLog("clickedCancelbutton")
176
-
177
- }
178
-
179
- }
180
-
181
-
182
-
183
- class MasterViewController: UITableViewController, AddItemViewControllerDelegate{
184
-
185
-
186
-
187
- var detailViewController: DetailViewController? = nil
188
-
189
- var objects = [Any]()
190
-
191
-
192
-
193
-
194
-
195
- override func viewDidLoad() {
196
-
197
- super.viewDidLoad()
198
-
199
- // Do any additional setup after loading the view, typically from a nib.
200
-
201
- navigationItem.leftBarButtonItem = editButtonItem
202
-
203
-
204
-
205
- //let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
206
-
207
- //navigationItem.rightBarButtonItem = addButton
208
-
209
- if let split = splitViewController {
210
-
211
- let controllers = split.viewControllers
212
-
213
- detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
214
-
215
- }
216
-
217
- }
218
-
219
-
220
-
221
- override func viewWillAppear(_ animated: Bool) {
222
-
223
- clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
224
-
225
- super.viewWillAppear(animated)
226
-
227
- }
228
-
229
-
230
-
231
- override func didReceiveMemoryWarning() {
232
-
233
- super.didReceiveMemoryWarning()
234
-
235
- // Dispose of any resources that can be recreated.
236
-
237
- }
238
-
239
-
240
-
241
- @objc
242
-
243
- func insertNewObject(_ sender: Any) {
244
-
245
- objects.insert(NSDate(), at: 0)
246
-
247
- let indexPath = IndexPath(row: 0, section: 0)
248
-
249
- tableView.insertRows(at: [indexPath], with: .automatic)
250
-
251
- }
252
-
253
-
254
-
255
- // MARK: - Segues
256
-
257
-
258
-
259
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
260
-
261
- if segue.identifier == "showDetail" {
262
-
263
- if let indexPath = tableView.indexPathForSelectedRow {
264
-
265
- let object = objects[indexPath.row] as! NSDate
266
-
267
- let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
268
-
269
- controller.detailItem = object
270
-
271
- controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
272
-
273
- controller.navigationItem.leftItemsSupplementBackButton = true
274
-
275
- }
276
-
277
- }
278
-
279
- }
280
-
281
-
282
-
283
- // MARK: - Table View
284
-
285
-
286
-
287
- override func numberOfSections(in tableView: UITableView) -> Int {
288
-
289
- return 1
290
-
291
- }
292
-
293
-
294
-
295
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
296
-
297
- return objects.count
298
-
299
- }
300
-
301
-
302
-
303
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
304
-
305
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
306
-
307
-
308
-
309
- let object = objects[indexPath.row] as! NSDate
310
-
311
- cell.textLabel!.text = object.description
312
-
313
- return cell
314
-
315
- }
316
-
317
-
318
-
319
- override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
320
-
321
- // Return false if you do not want the specified item to be editable.
322
-
323
- return true
324
-
325
- }
326
-
327
-
328
-
329
- override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
330
-
331
- if editingStyle == .delete {
332
-
333
- objects.remove(at: indexPath.row)
334
-
335
- tableView.deleteRows(at: [indexPath], with: .fade)
336
-
337
- } else if editingStyle == .insert {
338
-
339
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
340
-
341
- }
342
-
343
- }
344
-
345
-
346
-
347
- func addItemViewControllerDidCancel(modalText: String){
348
-
349
- print(modalText)
350
-
351
- NSLog("addItemViewControllerDidCancel")
352
-
353
- }
354
-
355
-
356
-
357
- }
358
-
359
-
360
-
361
- ```
362
-
363
273
 
364
274
 
365
275
  ### 試したこと