アクションは実際には通常のメソッドと同じもので、何か動作をさせるには{}
内に処理を記述しなければなりません。
ボタンを押したときに実行したいのはonClick
でしょうか?それでしたら、その内容をButtonCamera
内に移す必要があるかと思います。
あるいは、onClick
の頭に@IBAction
を付けて、ButtonCamera
の代わりにこちらをボタンと接続してもいいかもしれません。
[コメントに関して追記]
「中身を移す」の申し上げたのはfunc onClick(sender:UIButton){
と}
で挟まれた部分を@IBAction func ButtonCamera(_ sender: Any) {
と}
で挟まれた部分に移動するということを意図しました。
さらに、ButtonCameraの中でsenderのtagを見て分岐していますので、tagを見ることができるようsenderの型をAnyからUIButtonにしてみました。
このアクションを、ButtonCameraとButtomReadに対応する2つのボタンに繋いでみてください(ButtonCameraとButtomReadのどちらを押しても同じアクションが実行されるようにする)。
Swift
1import UIKit
2
3class SendController:UIViewController,
4UINavigationControllerDelegate,UIImagePickerControllerDelegate{
5
6 //このアクションとボタンを繋ぐ
7 @IBAction func ButtonCamera(_ sender: UIButton) { // senderの型をUIButtonに変更
8 if sender.tag == ButtonCamera {
9 showAlert(title: nil, text: "利用できません")
10 openPicker(sourceType: UIImagePickerControllerSourceType.camera)
11
12 }else if sender.tag == ButtomRead {
13 openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary)
14 }
15 }
16
17 //定数
18 let ButtonCamera = 0
19 let ButtomRead = 1
20 let ButtonWrite = 2
21
22
23 //変数
24 var imageView:UIImageView = UIImageView()
25 var btnCamera:UIButton = UIButton(type: .custom)
26 var btnRead:UIButton = UIButton(type: .custom)
27 var btnWrite:UIButton = UIButton(type: .custom)
28
29 //ロード完了時に呼ばれる
30 override func viewDidLoad() {
31 super.viewDidLoad()
32 }
33
34 //ボタンクリック時に呼ばれる
35 //以下のメソッドの中身をButtonCameraに移動
36 /*
37 func onClick(sender:UIButton){
38 if sender.tag == ButtonCamera {
39 showAlert(title: nil, text: "利用できません")
40 openPicker(sourceType: UIImagePickerControllerSourceType.camera)
41
42
43 }else if sender.tag == ButtomRead {
44 openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary)
45 }
46
47 }
48 */
49
50 //アラートの表示
51 func showAlert(title: String?, text: String?) {
52 let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert)
53 alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
54 present(alert, animated: true, completion: nil)
55 }
56
57 //イメージピッカーのオープン
58 func openPicker(sourceType:UIImagePickerControllerSourceType){
59 if !UIImagePickerController.isSourceTypeAvailable(sourceType){
60 showAlert(title: nil, text: "利用できません")
61 return
62 }
63
64 //イメージピッカーの生成
65 let picker = UIImagePickerController()
66 picker.sourceType = sourceType
67 picker.delegate = self
68
69 //ビューコントローラーのビューを開く
70 present(picker, animated: true, completion: nil)
71 }
72
73 // // イメージピッカーのイメージ取得時に呼ばれる
74 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
75 let image = info[UIImagePickerControllerOriginalImage]as! UIImage
76 imageView.image = image
77 //ビューコントローラーのビューを閉じる
78 picker.presentingViewController?.dismiss(animated: true,completion:nil)
79 }
80
81 // //イメージピッカーのキャンセル取得時に呼ばれる
82 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
83 picker.presentingViewController?.dismiss(animated: true, completion: nil)
84 }
85
86
87}
onClickをアクションに変えるパターンはこのような感じです。
Swift
1import UIKit
2
3class SendController:UIViewController,
4UINavigationControllerDelegate,UIImagePickerControllerDelegate{
5
6 //これは削除
7 /*
8 @IBAction func ButtonCamera(_ sender: Any) {
9 }
10 */
11
12 //定数
13 let ButtonCamera = 0
14 let ButtomRead = 1
15 let ButtonWrite = 2
16
17
18 //変数
19 var imageView:UIImageView = UIImageView()
20 var btnCamera:UIButton = UIButton(type: .custom)
21 var btnRead:UIButton = UIButton(type: .custom)
22 var btnWrite:UIButton = UIButton(type: .custom)
23
24 //ロード完了時に呼ばれる
25 override func viewDidLoad() {
26 super.viewDidLoad()
27 }
28
29 //ボタンクリック時に呼ばれる
30 @IBAction func onClick(sender:UIButton){ // @IBActionを追加
31 if sender.tag == ButtonCamera {
32 showAlert(title: nil, text: "利用できません")
33 openPicker(sourceType: UIImagePickerControllerSourceType.camera)
34
35
36 }else if sender.tag == ButtomRead {
37 openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary)
38 }
39
40 }
41
42 //アラートの表示
43 func showAlert(title: String?, text: String?) {
44 let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert)
45 alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
46 present(alert, animated: true, completion: nil)
47 }
48
49 //イメージピッカーのオープン
50 func openPicker(sourceType:UIImagePickerControllerSourceType){
51 if !UIImagePickerController.isSourceTypeAvailable(sourceType){
52 showAlert(title: nil, text: "利用できません")
53 return
54 }
55
56 //イメージピッカーの生成
57 let picker = UIImagePickerController()
58 picker.sourceType = sourceType
59 picker.delegate = self
60
61 //ビューコントローラーのビューを開く
62 present(picker, animated: true, completion: nil)
63 }
64
65 // // イメージピッカーのイメージ取得時に呼ばれる
66 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
67 let image = info[UIImagePickerControllerOriginalImage]as! UIImage
68 imageView.image = image
69 //ビューコントローラーのビューを閉じる
70 picker.presentingViewController?.dismiss(animated: true,completion:nil)
71 }
72
73 // //イメージピッカーのキャンセル取得時に呼ばれる
74 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
75 picker.presentingViewController?.dismiss(animated: true, completion: nil)
76 }
77
78
79}
こちらも同じく、ButtonCameraとButtomReadの両方のボタンに接続してみてください。
注意点として、ストーリーボード上のButtonCameraボタンが削除したアクションButtonCameraに繋がったままだと、ボタンを押した時にonClickとButtonCameraの両方のアクションを実行しようとしてクラッシュすると思いますので、コネクションインスペクタで古いアクションへの接続は切ってください。
このような感じでいかがでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/06/04 12:42
退会済みユーザー
2017/06/04 12:43
退会済みユーザー
2017/06/04 12:44
2017/06/04 21:08