質問編集履歴
1
内容の変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【Swift】
|
1
|
+
【Swift】Delegateがうまく機能しません
|
body
CHANGED
@@ -1,141 +1,44 @@
|
|
1
1
|
初めて質問させていただきます。
|
2
2
|
遷移先のViewのCollectionViewのCellに入っているそれぞれのImageViewを押したら遷移元のViewに戻り遷移元のImageViewに選んだ画像が反映されるようにしたいです。
|
3
|
-
|
3
|
+
delegateを使って実装しようとしましたがうまくできませんでした。
|
4
|
+
アプリ自体はStudyPlusのようなアプリをイメージしています。
|
4
5
|
|
6
|
+
### 遷移元のソースコード
|
5
7
|
```Swift
|
6
8
|
import UIKit
|
7
9
|
|
8
|
-
class addMaterialViewController:UIViewController{
|
9
|
-
|
10
|
-
@IBOutlet weak var addImageStackView: UIStackView!
|
11
|
-
@IBOutlet weak var addMaterialImageView: UIImageView!//ここに選択された画像を表示したい
|
12
|
-
@IBOutlet weak var materialName: UITextField!
|
13
|
-
@IBAction func tappedMaterialName(_ sender: Any) {
|
14
|
-
|
15
|
-
}
|
16
|
-
|
17
|
-
@IBOutlet weak var addCategory: UIStackView!
|
18
|
-
|
19
|
-
override func viewDidLoad() {
|
20
|
-
|
21
|
-
super.viewDidLoad()
|
22
|
-
|
23
|
-
materialName.delegate = self
|
24
|
-
|
25
|
-
//stackViewがタップされた時に処理を実行
|
26
|
-
let tapGestrureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tappedStackView))
|
27
|
-
self.addImageStackView.addGestureRecognizer(tapGestrureRecognizer)
|
28
|
-
|
29
|
-
|
10
|
+
protocol didChoiseMaterialDelegate {
|
30
|
-
|
31
|
-
|
11
|
+
|
32
|
-
|
33
|
-
//stackViewがタップされた時の処理
|
34
|
-
|
12
|
+
func changeImage()
|
35
|
-
|
13
|
+
|
36
|
-
|
37
|
-
let iconChoice = UIAlertAction(title: "アイコンから選択", style: .default, handler: {(action:UIAlertAction!) -> Void in
|
38
|
-
|
39
|
-
print("アイコンから選択します")
|
40
|
-
|
41
|
-
let storyboard = UIStoryboard(name: "choiseIcons", bundle: nil)
|
42
|
-
let choiseIconsViewController = storyboard.instantiateViewController(withIdentifier: "choiseIconsViewController") as! choiseIconsViewController
|
43
|
-
let nav = UINavigationController(rootViewController: choiseIconsViewController)
|
44
|
-
nav.modalTransitionStyle = .coverVertical
|
45
|
-
|
46
|
-
選択画面に遷移
|
47
|
-
self.present(nav, animated: false, completion: nil)
|
48
|
-
|
49
|
-
//画面遷移を横からに変更
|
50
|
-
let transition = CATransition()
|
51
|
-
transition.duration = 0.25
|
52
|
-
transition.type = CATransitionType.push
|
53
|
-
transition.subtype = CATransitionSubtype.fromRight
|
54
|
-
self.view.window!.layer.add(transition, forKey: kCATransition)
|
55
|
-
})
|
56
|
-
|
57
|
-
let TakeChoise = UIAlertAction(title: "写真を撮る", style: .default, handler: {(action:UIAlertAction!) -> Void in
|
58
|
-
print("写真を撮ります")
|
59
|
-
})
|
60
|
-
|
61
|
-
let LibraryChoise = UIAlertAction(title: "ライブラリから選択", style: .default, handler: {(action:UIAlertAction) -> Void in
|
62
|
-
print("ライブラリから選択します")
|
63
|
-
})
|
64
|
-
|
65
|
-
let cancel = UIAlertAction(title: "キャンセル", style: .cancel, handler: {(action:UIAlertAction!) -> Void in
|
66
|
-
print("キャンセルします")
|
67
|
-
choiseIamgeTypeAlert.dismiss(animated: false, completion: nil)
|
68
|
-
})
|
69
|
-
|
70
|
-
choiseIamgeTypeAlert.addAction(iconChoice)
|
71
|
-
choiseIamgeTypeAlert.addAction(TakeChoise)
|
72
|
-
choiseIamgeTypeAlert.addAction(LibraryChoise)
|
73
|
-
choiseIamgeTypeAlert.addAction(cancel)
|
74
|
-
|
75
|
-
self.present(choiseIamgeTypeAlert, animated: true, completion: nil)
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
80
14
|
}
|
81
|
-
```
|
82
|
-
### 遷移先のソースコード
|
83
15
|
|
84
|
-
```Swift
|
85
|
-
import UIKit
|
86
|
-
|
87
|
-
class choiseIconsViewController: UIViewController
|
16
|
+
class choiseIconsViewController: UIViewController{
|
88
17
|
|
89
|
-
private let cellId = "cellId"
|
90
18
|
|
91
|
-
|
92
|
-
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"]
|
19
|
+
var delegate: didChoiseMaterialDelegate?
|
93
20
|
|
94
|
-
|
21
|
+
〜省略〜
|
95
22
|
|
96
|
-
override func viewDidLoad() {
|
97
|
-
super.viewDidLoad()
|
98
|
-
|
99
|
-
iconsCollection.delegate = self
|
100
|
-
iconsCollection.dataSource = self
|
101
|
-
|
102
|
-
title = "アイコンから選択"
|
103
|
-
let backButtonItem = UIBarButtonItem(title: "戻る", style: .plain, target: self, action: #selector(tappedBack))
|
104
|
-
|
23
|
+
let appDlegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
105
|
-
|
106
|
-
}
|
107
24
|
|
108
|
-
//戻るボタンを押した時の処理
|
109
|
-
@objc func tappedBack() {
|
110
|
-
self.dismiss(animated: false, completion: nil)
|
111
|
-
let transition = CATransition()
|
112
|
-
transition.duration = 0.25
|
113
|
-
transition.type = CATransitionType.push
|
114
|
-
transition.subtype = CATransitionSubtype.fromLeft
|
115
|
-
self.view.window!.layer.add(transition, forKey: kCATransition)
|
116
|
-
}
|
117
|
-
|
118
25
|
}
|
119
26
|
|
120
27
|
extension choiseIconsViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
|
121
28
|
|
29
|
+
//cellが押された時の処理
|
122
30
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
123
31
|
|
32
|
+
self.delegate?.changeImage()
|
33
|
+
|
124
34
|
self.dismiss(animated: false, completion: nil)
|
125
35
|
|
126
|
-
let transition = CATransition()
|
127
|
-
transition.duration = 0.25
|
128
|
-
transition.type = CATransitionType.push
|
129
|
-
transition.subtype = CATransitionSubtype.fromLeft
|
130
|
-
self.view.window!.layer.add(transition, forKey: kCATransition)
|
131
|
-
|
132
36
|
}
|
133
37
|
|
134
38
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
135
39
|
return icons.count
|
136
40
|
}
|
137
41
|
|
138
|
-
//各CollectionViewのcellが押された時の処理
|
139
42
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
140
43
|
let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
|
141
44
|
|
@@ -145,47 +48,58 @@
|
|
145
48
|
let resizedImage = cellImage?.resized(withPercentage: 0.05)//画像のデータサイズを縮小
|
146
49
|
imageView.image = resizedImage
|
147
50
|
|
51
|
+
appDlegate.iconImage = imageView.image
|
52
|
+
|
148
53
|
return cell
|
149
54
|
}
|
150
55
|
|
56
|
+
〜省略〜
|
57
|
+
}
|
58
|
+
```
|
151
|
-
|
59
|
+
### 遷移先のソースコード
|
60
|
+
|
61
|
+
```Swift
|
62
|
+
import UIKit
|
63
|
+
|
152
|
-
|
64
|
+
class addMaterialViewController:UIViewController,didChoiseMaterialDelegate{
|
65
|
+
|
66
|
+
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
|
67
|
+
|
68
|
+
@IBOutlet weak var addImageStackView: UIStackView!
|
69
|
+
@IBOutlet weak var addMaterialImageView: UIImageView!
|
70
|
+
@IBOutlet weak var materialName: UITextField!
|
71
|
+
@IBAction func tappedMaterialName(_ sender: Any) {
|
153
72
|
|
154
|
-
return CGSize(width: 20, height: 40)
|
155
|
-
|
156
73
|
}
|
157
74
|
|
75
|
+
@IBOutlet weak var addCategory: UIStackView!
|
76
|
+
|
158
|
-
|
77
|
+
override func viewDidLoad() {
|
159
|
-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
160
78
|
|
161
|
-
|
79
|
+
super.viewDidLoad()
|
162
80
|
|
81
|
+
materialName.delegate = self
|
82
|
+
|
83
|
+
//stackViewがタップされた時に処理を実行
|
84
|
+
let tapGestrureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tappedStackView))
|
85
|
+
self.addImageStackView.addGestureRecognizer(tapGestrureRecognizer)
|
86
|
+
|
87
|
+
addMaterialImageView.image = appDelegate.iconImage
|
88
|
+
|
163
89
|
}
|
164
90
|
|
165
|
-
|
91
|
+
func changeImage() {
|
166
|
-
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
167
|
-
|
92
|
+
print("success")
|
93
|
+
addMaterialImageView.image = appDelegate.iconImage
|
168
94
|
}
|
169
95
|
|
170
|
-
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
171
|
-
|
96
|
+
〜省略〜
|
172
|
-
}
|
173
|
-
|
174
97
|
}
|
175
|
-
|
176
|
-
extension UIImage {
|
177
|
-
func resized(withPercentage percentage: CGFloat) -> UIImage? {
|
178
|
-
let canvas = CGSize(width: size.width * percentage, height: size.height * percentage)
|
179
|
-
return UIGraphicsImageRenderer(size: canvas, format: imageRendererFormat).image {
|
180
|
-
_ in draw(in: CGRect(origin: .zero, size: canvas))
|
181
|
-
}
|
182
|
-
}
|
183
|
-
}
|
184
98
|
```
|
185
99
|
|
186
100
|
### 試したこと
|
187
101
|
|
188
|
-
|
102
|
+
ネットでdelegateの実装方法を調ベて試した
|
189
103
|
|
190
104
|
### 補足情報
|
191
105
|
|