質問編集履歴
1
内容の変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
【Swift】
|
1
|
+
【Swift】Delegateがうまく機能しません
|
test
CHANGED
@@ -2,9 +2,13 @@
|
|
2
2
|
|
3
3
|
遷移先のViewのCollectionViewのCellに入っているそれぞれのImageViewを押したら遷移元のViewに戻り遷移元のImageViewに選んだ画像が反映されるようにしたいです。
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
delegateを使って実装しようとしましたがうまくできませんでした。
|
6
|
+
|
7
|
+
アプリ自体はStudyPlusのようなアプリをイメージしています。![![イメージ説明](c9272ca26aaeee4b3138a82b16572ffc.jpeg)![イメージ説明](5e2822a361fa1ac26683f494ada410bd.jpeg)![イメージ説明](6a1e2a485653ce292a3cd16d4a614d5f.jpeg)
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
### 遷移元のソースコード
|
8
12
|
|
9
13
|
```Swift
|
10
14
|
|
@@ -12,13 +16,125 @@
|
|
12
16
|
|
13
17
|
|
14
18
|
|
19
|
+
protocol didChoiseMaterialDelegate {
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
func changeImage()
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
|
30
|
+
|
15
|
-
class
|
31
|
+
class choiseIconsViewController: UIViewController{
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
var delegate: didChoiseMaterialDelegate?
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
〜省略〜
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
let appDlegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
extension choiseIconsViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
//cellが押された時の処理
|
58
|
+
|
59
|
+
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
self.delegate?.changeImage()
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
self.dismiss(animated: false, completion: nil)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
76
|
+
|
77
|
+
return icons.count
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
84
|
+
|
85
|
+
let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
let imageView = cell.contentView.viewWithTag(1) as! UIImageView
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
let cellImage = UIImage(named: icons[indexPath.row])//各セルにiconsの画像を反映
|
94
|
+
|
95
|
+
let resizedImage = cellImage?.resized(withPercentage: 0.05)//画像のデータサイズを縮小
|
96
|
+
|
97
|
+
imageView.image = resizedImage
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
appDlegate.iconImage = imageView.image
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
return cell
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
〜省略〜
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
### 遷移先のソースコード
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```Swift
|
122
|
+
|
123
|
+
import UIKit
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
class addMaterialViewController:UIViewController,didChoiseMaterialDelegate{
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
16
132
|
|
17
133
|
|
18
134
|
|
19
135
|
@IBOutlet weak var addImageStackView: UIStackView!
|
20
136
|
|
21
|
-
@IBOutlet weak var addMaterialImageView: UIImageView!
|
137
|
+
@IBOutlet weak var addMaterialImageView: UIImageView!
|
22
138
|
|
23
139
|
@IBOutlet weak var materialName: UITextField!
|
24
140
|
|
@@ -54,325 +170,37 @@
|
|
54
170
|
|
55
171
|
|
56
172
|
|
57
|
-
|
173
|
+
addMaterialImageView.image = appDelegate.iconImage
|
58
|
-
|
59
|
-
|
60
|
-
|
174
|
+
|
175
|
+
|
176
|
+
|
61
|
-
}
|
177
|
+
}
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
178
|
+
|
66
|
-
|
179
|
+
|
180
|
+
|
67
|
-
|
181
|
+
func changeImage() {
|
68
|
-
|
69
|
-
|
182
|
+
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
let iconChoice = UIAlertAction(title: "アイコンから選択", style: .default, handler: {(action:UIAlertAction!) -> Void in
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
print("アイコンから選択します")
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
let storyboard = UIStoryboard(name: "choiseIcons", bundle: nil)
|
82
|
-
|
83
|
-
let choiseIconsViewController = storyboard.instantiateViewController(withIdentifier: "choiseIconsViewController") as! choiseIconsViewController
|
84
|
-
|
85
|
-
let nav = UINavigationController(rootViewController: choiseIconsViewController)
|
86
|
-
|
87
|
-
nav.modalTransitionStyle = .coverVertical
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
選択画面に遷移
|
92
|
-
|
93
|
-
self.present(nav, animated: false, completion: nil)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
//画面遷移を横からに変更
|
98
|
-
|
99
|
-
let transition = CATransition()
|
100
|
-
|
101
|
-
transition.duration = 0.25
|
102
|
-
|
103
|
-
transition.type = CATransitionType.push
|
104
|
-
|
105
|
-
transition.subtype = CATransitionSubtype.fromRight
|
106
|
-
|
107
|
-
self.view.window!.layer.add(transition, forKey: kCATransition)
|
108
|
-
|
109
|
-
})
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
let TakeChoise = UIAlertAction(title: "写真を撮る", style: .default, handler: {(action:UIAlertAction!) -> Void in
|
114
|
-
|
115
|
-
|
183
|
+
print("success")
|
116
|
-
|
117
|
-
|
184
|
+
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
let LibraryChoise = UIAlertAction(title: "ライブラリから選択", style: .default, handler: {(action:UIAlertAction) -> Void in
|
122
|
-
|
123
|
-
print("ライブラリから選択します")
|
124
|
-
|
125
|
-
})
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
let cancel = UIAlertAction(title: "キャンセル", style: .cancel, handler: {(action:UIAlertAction!) -> Void in
|
130
|
-
|
131
|
-
print("キャンセルします")
|
132
|
-
|
133
|
-
choiseIamgeTypeAlert.dismiss(animated: false, completion: nil)
|
134
|
-
|
135
|
-
})
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
185
|
+
addMaterialImageView.image = appDelegate.iconImage
|
140
|
-
|
141
|
-
|
186
|
+
|
142
|
-
|
143
|
-
choiseIamgeTypeAlert.addAction(LibraryChoise)
|
144
|
-
|
145
|
-
choiseIamgeTypeAlert.addAction(cancel)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
self.present(choiseIamgeTypeAlert, animated: true, completion: nil)
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
}
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
〜省略〜
|
158
192
|
|
159
193
|
}
|
160
194
|
|
161
195
|
```
|
162
196
|
|
163
|
-
### 遷移先のソースコード
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
```Swift
|
168
|
-
|
169
|
-
import UIKit
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
class choiseIconsViewController: UIViewController {
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
private let cellId = "cellId"
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
//画像の名前
|
182
|
-
|
183
|
-
let icons = ["tb01","tb02","tb03","tb04","tb05","tb06","tb07","tb08","tb09","tb10","tb11","tb12","tb13","tb14","tb15","tb16","tb17","pr01","pr02","pr03","pr04","pr05","pr06","pr07","pr08","pr09","pr10","pr11","pr12","pr13","pr14","pr15"]
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
@IBOutlet weak var iconsCollection: UICollectionView!
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
override func viewDidLoad() {
|
192
|
-
|
193
|
-
super.viewDidLoad()
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
iconsCollection.delegate = self
|
198
|
-
|
199
|
-
iconsCollection.dataSource = self
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
title = "アイコンから選択"
|
204
|
-
|
205
|
-
let backButtonItem = UIBarButtonItem(title: "戻る", style: .plain, target: self, action: #selector(tappedBack))
|
206
|
-
|
207
|
-
self.navigationItem.leftBarButtonItem = backButtonItem
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
}
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
//戻るボタンを押した時の処理
|
216
|
-
|
217
|
-
@objc func tappedBack() {
|
218
|
-
|
219
|
-
self.dismiss(animated: false, completion: nil)
|
220
|
-
|
221
|
-
let transition = CATransition()
|
222
|
-
|
223
|
-
transition.duration = 0.25
|
224
|
-
|
225
|
-
transition.type = CATransitionType.push
|
226
|
-
|
227
|
-
transition.subtype = CATransitionSubtype.fromLeft
|
228
|
-
|
229
|
-
self.view.window!.layer.add(transition, forKey: kCATransition)
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
}
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
extension choiseIconsViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
self.dismiss(animated: false, completion: nil)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
let transition = CATransition()
|
252
|
-
|
253
|
-
transition.duration = 0.25
|
254
|
-
|
255
|
-
transition.type = CATransitionType.push
|
256
|
-
|
257
|
-
transition.subtype = CATransitionSubtype.fromLeft
|
258
|
-
|
259
|
-
self.view.window!.layer.add(transition, forKey: kCATransition)
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
268
|
-
|
269
|
-
return icons.count
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
//各CollectionViewのcellが押された時の処理
|
276
|
-
|
277
|
-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
278
|
-
|
279
|
-
let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
let imageView = cell.contentView.viewWithTag(1) as! UIImageView
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
let cellImage = UIImage(named: icons[indexPath.row])//各セルにiconsの画像を反映
|
288
|
-
|
289
|
-
let resizedImage = cellImage?.resized(withPercentage: 0.05)//画像のデータサイズを縮小
|
290
|
-
|
291
|
-
imageView.image = resizedImage
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
return cell
|
296
|
-
|
297
|
-
}
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
//セルのサイズの指定
|
302
|
-
|
303
|
-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
return CGSize(width: 20, height: 40)
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
//行の間隔
|
316
|
-
|
317
|
-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
return 30
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
//列の間隔
|
330
|
-
|
331
|
-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
332
|
-
|
333
|
-
return 30
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
340
|
-
|
341
|
-
return 1
|
342
|
-
|
343
|
-
}
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
}
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
extension UIImage {
|
352
|
-
|
353
|
-
func resized(withPercentage percentage: CGFloat) -> UIImage? {
|
354
|
-
|
355
|
-
let canvas = CGSize(width: size.width * percentage, height: size.height * percentage)
|
356
|
-
|
357
|
-
return UIGraphicsImageRenderer(size: canvas, format: imageRendererFormat).image {
|
358
|
-
|
359
|
-
_ in draw(in: CGRect(origin: .zero, size: canvas))
|
360
|
-
|
361
|
-
}
|
362
|
-
|
363
|
-
}
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
```
|
368
|
-
|
369
197
|
|
370
198
|
|
371
199
|
### 試したこと
|
372
200
|
|
373
201
|
|
374
202
|
|
375
|
-
|
203
|
+
ネットでdelegateの実装方法を調ベて試した
|
376
204
|
|
377
205
|
|
378
206
|
|