質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2330閲覧

@IBAction funcを書く位置が間違っているのか?

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/06/04 02:35

@IBAction funcを書く位置が間違っているのでしょうか?
SendControllerに

import UIKit class SendController:UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate{ @IBAction func ButtonCamera(_ sender: Any) { } //定数 let ButtonCamera = 0 let ButtomRead = 1 let ButtonWrite = 2 //変数 var imageView:UIImageView = UIImageView() var btnCamera:UIButton = UIButton(type: .custom) var btnRead:UIButton = UIButton(type: .custom) var btnWrite:UIButton = UIButton(type: .custom) //ロード完了時に呼ばれる override func viewDidLoad() { super.viewDidLoad() } //ボタンクリック時に呼ばれる func onClick(sender:UIButton){ if sender.tag == ButtonCamera { showAlert(title: nil, text: "利用できません") openPicker(sourceType: UIImagePickerControllerSourceType.camera) }else if sender.tag == ButtomRead { openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary) } } //アラートの表示 func showAlert(title: String?, text: String?) { let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) present(alert, animated: true, completion: nil) } //イメージピッカーのオープン func openPicker(sourceType:UIImagePickerControllerSourceType){ if !UIImagePickerController.isSourceTypeAvailable(sourceType){ showAlert(title: nil, text: "利用できません") return } //イメージピッカーの生成 let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = self //ビューコントローラーのビューを開く present(picker, animated: true, completion: nil) } // // イメージピッカーのイメージ取得時に呼ばれる func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage]as! UIImage imageView.image = image //ビューコントローラーのビューを閉じる picker.presentingViewController?.dismiss(animated: true,completion:nil) } // //イメージピッカーのキャンセル取得時に呼ばれる func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.presentingViewController?.dismiss(animated: true, completion: nil) } }

ButtonCameraと名付けた”写真を撮る”ボタンを押すと、カメラが起動するようにしたいです。MainStoryBoardのそのボタンをコントロールキーを押しながら

@IBAction func ButtonCamera(_ sender: Any) { }

の部分でドラックアンドドロップしました。
その時のConnection欄をActionにし@IBAction func ButtonCamera〜のコードを追加しました。
ですが、自分のiPhoneでアプリを立ち上げて、”写真を撮る”ボタンを押してもカメラが起動しません。
@IBAction func ButtonCamera〜書く位置が間違っているのか、と思いましたがどう直せばいいのかわからず。
どう直せば良いのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

アクションは実際には通常のメソッドと同じもので、何か動作をさせるには{}内に処理を記述しなければなりません。
ボタンを押したときに実行したいのは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 03:31

編集2017/06/04 20:54
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2017/06/04 12:42

ありがとうございます。
退会済みユーザー

退会済みユーザー

2017/06/04 12:43

import UIKit class KenshinSendController:UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate{ @IBAction func ButtonCamera(_ sender: Any) { //イメージピッカーのオープン func openPicker(sourceType:UIImagePickerControllerSourceType){ if !UIImagePickerController.isSourceTypeAvailable(sourceType){ showAlert(title: nil, text: "利用できません") return } //イメージピッカーの生成 let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = self //ビューコントローラーのビューを開く present(picker, animated: true, completion: nil) } } //定数 let ButtonCamera = 0 let ButtomRead = 1 let ButtonWrite = 2 //変数 var imageView:UIImageView = UIImageView() var btnCamera:UIButton = UIButton(type: .custom) var btnRead:UIButton = UIButton(type: .custom) var btnWrite:UIButton = UIButton(type: .custom) //ロード完了時に呼ばれる override func viewDidLoad() { super.viewDidLoad() } //アラートの表示 func showAlert(title: String?, text: String?) { let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) present(alert, animated: true, completion: nil) } // // イメージピッカーのイメージ取得時に呼ばれる func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage]as! UIImage imageView.image = image //ビューコントローラーのビューを閉じる picker.presentingViewController?.dismiss(animated: true,completion:nil) } // //イメージピッカーのキャンセル取得時に呼ばれる func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.presentingViewController?.dismiss(animated: true, completion: nil) } }
退会済みユーザー

退会済みユーザー

2017/06/04 12:44

と実行したいonClickをButtonCamera内に移しました。しかし、動きません。私の解釈が間違っているのでしょうか?
Bongo

2017/06/04 21:08

図を挿入するため回答欄の方に追記しました。 ちなみに質問者さんの記述ですと、「ButtonCameraメソッドの内部でopenPickerメソッドを定義する」というような形になり、openPickerメソッドは定義されるのみで実行されず動作しなかったものと思われます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問