回答編集履歴

1

図とコードを入れるためコメント欄に代わって回答欄に返信

2017/06/04 20:54

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -3,3 +3,381 @@
3
3
  ボタンを押したときに実行したいのは`onClick`でしょうか?それでしたら、その内容を`ButtonCamera`内に移す必要があるかと思います。
4
4
 
5
5
  あるいは、`onClick`の頭に`@IBAction`を付けて、`ButtonCamera`の代わりにこちらをボタンと接続してもいいかもしれません。
6
+
7
+
8
+
9
+ [コメントに関して追記]
10
+
11
+ 「中身を移す」の申し上げたのは`func onClick(sender:UIButton){`と`}`で挟まれた部分を`@IBAction func ButtonCamera(_ sender: Any) {`と`}`で挟まれた部分に移動するということを意図しました。
12
+
13
+ さらに、ButtonCameraの中でsenderのtagを見て分岐していますので、tagを見ることができるようsenderの型をAnyからUIButtonにしてみました。
14
+
15
+ このアクションを、ButtonCameraとButtomReadに対応する2つのボタンに繋いでみてください(ButtonCameraとButtomReadのどちらを押しても同じアクションが実行されるようにする)。
16
+
17
+
18
+
19
+ ```Swift
20
+
21
+ import UIKit
22
+
23
+
24
+
25
+ class SendController:UIViewController,
26
+
27
+ UINavigationControllerDelegate,UIImagePickerControllerDelegate{
28
+
29
+
30
+
31
+ //このアクションとボタンを繋ぐ
32
+
33
+ @IBAction func ButtonCamera(_ sender: UIButton) { // senderの型をUIButtonに変更
34
+
35
+ if sender.tag == ButtonCamera {
36
+
37
+ showAlert(title: nil, text: "利用できません")
38
+
39
+ openPicker(sourceType: UIImagePickerControllerSourceType.camera)
40
+
41
+
42
+
43
+ }else if sender.tag == ButtomRead {
44
+
45
+ openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary)
46
+
47
+ }
48
+
49
+ }
50
+
51
+
52
+
53
+ //定数
54
+
55
+ let ButtonCamera = 0
56
+
57
+ let ButtomRead = 1
58
+
59
+ let ButtonWrite = 2
60
+
61
+
62
+
63
+
64
+
65
+ //変数
66
+
67
+ var imageView:UIImageView = UIImageView()
68
+
69
+ var btnCamera:UIButton = UIButton(type: .custom)
70
+
71
+ var btnRead:UIButton = UIButton(type: .custom)
72
+
73
+ var btnWrite:UIButton = UIButton(type: .custom)
74
+
75
+
76
+
77
+ //ロード完了時に呼ばれる
78
+
79
+ override func viewDidLoad() {
80
+
81
+ super.viewDidLoad()
82
+
83
+ }
84
+
85
+
86
+
87
+ //ボタンクリック時に呼ばれる
88
+
89
+ //以下のメソッドの中身をButtonCameraに移動
90
+
91
+ /*
92
+
93
+ func onClick(sender:UIButton){
94
+
95
+ if sender.tag == ButtonCamera {
96
+
97
+ showAlert(title: nil, text: "利用できません")
98
+
99
+ openPicker(sourceType: UIImagePickerControllerSourceType.camera)
100
+
101
+
102
+
103
+
104
+
105
+ }else if sender.tag == ButtomRead {
106
+
107
+ openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary)
108
+
109
+ }
110
+
111
+
112
+
113
+ }
114
+
115
+ */
116
+
117
+
118
+
119
+ //アラートの表示
120
+
121
+ func showAlert(title: String?, text: String?) {
122
+
123
+ let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert)
124
+
125
+ alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
126
+
127
+ present(alert, animated: true, completion: nil)
128
+
129
+ }
130
+
131
+
132
+
133
+ //イメージピッカーのオープン
134
+
135
+ func openPicker(sourceType:UIImagePickerControllerSourceType){
136
+
137
+ if !UIImagePickerController.isSourceTypeAvailable(sourceType){
138
+
139
+ showAlert(title: nil, text: "利用できません")
140
+
141
+ return
142
+
143
+ }
144
+
145
+
146
+
147
+ //イメージピッカーの生成
148
+
149
+ let picker = UIImagePickerController()
150
+
151
+ picker.sourceType = sourceType
152
+
153
+ picker.delegate = self
154
+
155
+
156
+
157
+ //ビューコントローラーのビューを開く
158
+
159
+ present(picker, animated: true, completion: nil)
160
+
161
+ }
162
+
163
+
164
+
165
+ // // イメージピッカーのイメージ取得時に呼ばれる
166
+
167
+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
168
+
169
+ let image = info[UIImagePickerControllerOriginalImage]as! UIImage
170
+
171
+ imageView.image = image
172
+
173
+ //ビューコントローラーのビューを閉じる
174
+
175
+ picker.presentingViewController?.dismiss(animated: true,completion:nil)
176
+
177
+ }
178
+
179
+
180
+
181
+ // //イメージピッカーのキャンセル取得時に呼ばれる
182
+
183
+ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
184
+
185
+ picker.presentingViewController?.dismiss(animated: true, completion: nil)
186
+
187
+ }
188
+
189
+
190
+
191
+
192
+
193
+ }
194
+
195
+ ```
196
+
197
+
198
+
199
+ onClickをアクションに変えるパターンはこのような感じです。
200
+
201
+
202
+
203
+ ```Swift
204
+
205
+ import UIKit
206
+
207
+
208
+
209
+ class SendController:UIViewController,
210
+
211
+ UINavigationControllerDelegate,UIImagePickerControllerDelegate{
212
+
213
+
214
+
215
+ //これは削除
216
+
217
+ /*
218
+
219
+ @IBAction func ButtonCamera(_ sender: Any) {
220
+
221
+ }
222
+
223
+ */
224
+
225
+
226
+
227
+ //定数
228
+
229
+ let ButtonCamera = 0
230
+
231
+ let ButtomRead = 1
232
+
233
+ let ButtonWrite = 2
234
+
235
+
236
+
237
+
238
+
239
+ //変数
240
+
241
+ var imageView:UIImageView = UIImageView()
242
+
243
+ var btnCamera:UIButton = UIButton(type: .custom)
244
+
245
+ var btnRead:UIButton = UIButton(type: .custom)
246
+
247
+ var btnWrite:UIButton = UIButton(type: .custom)
248
+
249
+
250
+
251
+ //ロード完了時に呼ばれる
252
+
253
+ override func viewDidLoad() {
254
+
255
+ super.viewDidLoad()
256
+
257
+ }
258
+
259
+
260
+
261
+ //ボタンクリック時に呼ばれる
262
+
263
+ @IBAction func onClick(sender:UIButton){ // @IBActionを追加
264
+
265
+ if sender.tag == ButtonCamera {
266
+
267
+ showAlert(title: nil, text: "利用できません")
268
+
269
+ openPicker(sourceType: UIImagePickerControllerSourceType.camera)
270
+
271
+
272
+
273
+
274
+
275
+ }else if sender.tag == ButtomRead {
276
+
277
+ openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary)
278
+
279
+ }
280
+
281
+
282
+
283
+ }
284
+
285
+
286
+
287
+ //アラートの表示
288
+
289
+ func showAlert(title: String?, text: String?) {
290
+
291
+ let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert)
292
+
293
+ alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
294
+
295
+ present(alert, animated: true, completion: nil)
296
+
297
+ }
298
+
299
+
300
+
301
+ //イメージピッカーのオープン
302
+
303
+ func openPicker(sourceType:UIImagePickerControllerSourceType){
304
+
305
+ if !UIImagePickerController.isSourceTypeAvailable(sourceType){
306
+
307
+ showAlert(title: nil, text: "利用できません")
308
+
309
+ return
310
+
311
+ }
312
+
313
+
314
+
315
+ //イメージピッカーの生成
316
+
317
+ let picker = UIImagePickerController()
318
+
319
+ picker.sourceType = sourceType
320
+
321
+ picker.delegate = self
322
+
323
+
324
+
325
+ //ビューコントローラーのビューを開く
326
+
327
+ present(picker, animated: true, completion: nil)
328
+
329
+ }
330
+
331
+
332
+
333
+ // // イメージピッカーのイメージ取得時に呼ばれる
334
+
335
+ func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
336
+
337
+ let image = info[UIImagePickerControllerOriginalImage]as! UIImage
338
+
339
+ imageView.image = image
340
+
341
+ //ビューコントローラーのビューを閉じる
342
+
343
+ picker.presentingViewController?.dismiss(animated: true,completion:nil)
344
+
345
+ }
346
+
347
+
348
+
349
+ // //イメージピッカーのキャンセル取得時に呼ばれる
350
+
351
+ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
352
+
353
+ picker.presentingViewController?.dismiss(animated: true, completion: nil)
354
+
355
+ }
356
+
357
+
358
+
359
+
360
+
361
+ }
362
+
363
+ ```
364
+
365
+
366
+
367
+ こちらも同じく、ButtonCameraとButtomReadの両方のボタンに接続してみてください。
368
+
369
+
370
+
371
+ ![接続図](6defb01e779ee9e442fca1430bab76d9.png)
372
+
373
+
374
+
375
+ 注意点として、ストーリーボード上のButtonCameraボタンが削除したアクションButtonCameraに繋がったままだと、ボタンを押した時にonClickとButtonCameraの両方のアクションを実行しようとしてクラッシュすると思いますので、コネクションインスペクタで古いアクションへの接続は切ってください。
376
+
377
+
378
+
379
+ ![古い接続を切る](6cd01921271cd733b529ab19ae327320.png)
380
+
381
+
382
+
383
+ このような感じでいかがでしょうか?